On Sunday, August 13, 2017 at 6:18:54 PM UTC-4, jim kaubisch wrote:
>
> I have good reason to want to create a database using a web2py app, but 
> also access the db from a standalone app that does tons of data processing 
> but basically does not need/want any user interaction
> The web2py app makes sure all data is consistent, supports the user in 
> entering valid data, manages YouTube videos, and media maps on a website 
> etc - lots of reasons for an interactive app.
> The standalone app does LOTS of data processing on the definitions created 
> via the web2py app as well as other files. 
> Its interface (for non-tech people) is simply click on "Import", followed 
> by "Prepare" followed by "Deploy" - no UI wanted or needed (Logs are kept 
> of what happened)
>
> I’ve looked, but so far have not found a complete explanation of how to 
> access the same database both with web2py and standalone.
>
> 1. the book says;
>
>     "
>      The web2py DAL can be used in a non-web2py environment via
> 1. from gluon import DAL, Field
> 2. # also consider: from gluon.validators import *
>    "
>
>    YET in the standalone, shouldn’t it be 
>
> from pydal import DAL, Field ?
>

If you import from gluon, you get some extra web2py functionality attached 
to the DAL (JSON and XML serializers, default validators, default represent 
functions, and the web2py_uuid helper). You also get the database drivers 
available in /gluon/contrib.

2. Whichever I use, the db connection is made (good news) but, 
>     next problem I hit is that when I import my db model definition into 
> the standalone, I get:
>
> NameError: "name 'T' is not defined"
> module body in dev.pyDAL_test.py at line 8
> import mfm_media_model
> module body in mfm_media_model.py at line 73
> Field('db_version',   'string'     , label=T('DB Version')  , 
> writable=False),
>
>     ... and there would be others once I got past this one
>     I have found no explanation of how to access the definitions of T and 
> other helpers :(
>

T is part of the web2py global environment. You can create your own T if 
desired -- it is an instance of the translator class from gluon.languages 
(you must pass it a path to the language files folder as well as a default 
language).

It would be really helpful to have even a simple template of how to 
> implement access to a common database defined in web2py and standalone.
> I’ve looked and haven’t found one.
>
> 1. what to include in the model imports to define the standalone db access?
> from gluon? or from pydal? import DAL, Field? Or does it even matter?
>

See above.
 

>         2. What to include to import the same table definitions on both 
> sides, 
>              including the helpers defined in the book for table 
> definitions?
>

Validators are in gluon.validators and widgets are in gluon.sqlhtml.

If you want to share table definitions, you'll need to put them in a module 
somewhere (e.g., the application's /modules folder) and import where needed.

If you want to enable record versioning for all tables at once using 
auth.enable_record_versioning(), you will of course need Auth from 
gluon.tools. Otherwise, you can enable versioning for individual tables via 
db.mytable._enable_record_versioning().
 

> 3. What about the question of “migrate_enabled”. presumably 
>            only set to True in one app? What are the gotchas, if any?
>

Yes, probably best to enable migrations from only one place.
 

> 4. Is there anything else that’s required or, based on experience,
>            wise to do include/exclude/avoid etc to make things work
>

For upload fields, you may have to explicitly specify the "uploadfolder" 
argument to indicate where files should be uploaded.

All that having been said, you're probably much better off simply running 
your external program in the context of the web2py environment of your app 
via:

python web2py.py -S myapp -M -R /path/to/python/script.py

In that case, you don't have to worry about any of the above, as your 
script will be executed in the same environment as your application, 
include everything defined in your models as well as the web2py globals.

Another option is to use exec_environment 
<http://web2py.com/books/default/chapter/29/04/the-core#Execution-environment>. 
Actually, it's not documented, but the similar env 
<https://github.com/web2py/web2py/blob/master/gluon/shell.py#L97> function 
from gluon.shell might be more useful, as it lets you create an environment 
that includes the models of a specific application.

You might also consider just making a very simple web interface with the 
import/prepare/deploy workflow -- it can be part of the same app.

Anthony

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to