librelist archives

« back to archive

Re: [flask] Flask with MongoDB

Re: [flask] Flask with MongoDB

From:
Jörg Bönisch
Date:
2014-09-04 @ 04:42
Hi, 

Maybe this could help:
 http://flask.pocoo.org/docs/0.10/patterns/mongokit/

The modes depend on how you plan to deploy your app to a web server, since
the app.run() stuff is single threaded and for (local) development only.

P.S. Docs are great :-)

-------- Originalmitteilung --------
Von:Yaroslav Kyrpych <yaroslav.kyrpych@gmail.com>
Gesendet:Thu, 04 Sep 2014 05:56:35 +0200
An:flask@librelist.com
Betreff:[flask] Flask with MongoDB

>Hello!
>
>
>I am new to Flask and web development. I am working on application that 
uses MongoDB (pymongo). I am trying to set up config file. So far it looks
as follows:
>
>
>import os
>
>class Config(object):
> DEBUG = False
> TESTING = False
> CSRF_ENABLED = True
> SECRET_KEY = 'TBU'
> CSRF_SECRET_KEY=TBU'
> THREADS_PER_PAGE=2
>
>
>class ProductionConfig(Config):
>    DEBUG = False
>
>
>class StagingConfig(Config):
>    DEVELOPMENT = True
>    DEBUG = True
>
>
>class DevelopmentConfig(Config):
>    DEVELOPMENT = True
>    DEBUG = True
>
>
>class TestingConfig(Config):
>    TESTING = True
>
>
>I am bit unclear how I set path to a folder where mongodb saves data. 
Please note I have several databases that I are located in the same folder
(data), so I plan to make calls to specific database in a specific 
function for each page, I just want to import path to a folder with 
databases. My folder tree looks as follows now:
>
>
>C:\app\data - here all mongodb databases are located, i.e. data for application.
>
>C:\app\__init__.py
>
>C:\app\config.py
>
>C:\app\virtualenv.py
>
>C:\app\flask
>
>C:\app\static
>
>C:\app\templates
>
>C:\app\source_data
>
>C:\app\__pycache__
>
>
>__init__ file looks as follows:
>
>app=Flask(__name__)
>
>app.config.from_object(__name__)
>
>
>Another question is how do I specify which mode (development, production,
etc) to use.
>
>
>
>Any help is much appreciated.
>
>
>Thank you,
>
>
>Yaroslav
>

Re: [flask] Flask with MongoDB

From:
Yaroslav Kyrpych
Date:
2014-09-04 @ 05:15
Jörg,

Thanks for the link to docs.

I read docs about config file, and did not quite understand them,
specifically, it's still unclear to me how import happens from config.py to
__init__.py. Do I need to specify path to config.py? How does the whole
process of settings (config) importation work?

Have a great day,

Yaroslav


On Wed, Sep 3, 2014 at 11:42 PM, Jörg Bönisch <joerg@joergwork.org> wrote:

> Hi,
>
> Maybe this could help:
> http://flask.pocoo.org/docs/0.10/patterns/mongokit/
>
> The modes depend on how you plan to deploy your app to a web server, since
> the app.run() stuff is single threaded and for (local) development only.
>
> P.S. Docs are great :-)
>
> -------- Originalmitteilung --------
> Von:Yaroslav Kyrpych <yaroslav.kyrpych@gmail.com>
> Gesendet:Thu, 04 Sep 2014 05:56:35 +0200
> An:flask@librelist.com
> Betreff:[flask] Flask with MongoDB
>
> Hello!
>
> I am new to Flask and web development. I am working on application that
> uses MongoDB (pymongo). I am trying to set up config file. So far it looks
> as follows:
>
> import os
> class Config(object):
>  DEBUG = False
>  TESTING = False
>  CSRF_ENABLED = True
>  SECRET_KEY = 'TBU'
>  CSRF_SECRET_KEY=TBU'
>  THREADS_PER_PAGE=2
>
> class ProductionConfig(Config):
>     DEBUG = False
>
> class StagingConfig(Config):
>     DEVELOPMENT = True
>     DEBUG = True
>
> class DevelopmentConfig(Config):
>     DEVELOPMENT = True
>     DEBUG = True
>
> class TestingConfig(Config):
>     TESTING = True
>
> I am bit unclear how I set path to a folder where mongodb saves data.
> Please note I have several databases that I are located in the same folder
> (data), so I plan to make calls to specific database in a specific function
> for each page, I just want to import path to a folder with databases. My
> folder tree looks as follows now:
>
> C:\app\data - here all mongodb databases are located, i.e. data for
> application.
> C:\app\__init__.py
> C:\app\config.py
> C:\app\virtualenv.py
> C:\app\flask
> C:\app\static
> C:\app\templates
> C:\app\source_data
> C:\app\__pycache__
>
> __init__ file looks as follows:
> app=Flask(__name__)
> app.config.from_object(__name__)
>
> Another question is how do I specify which mode (development, production,
> etc) to use.
>
>
> Any help is much appreciated.
>
> Thank you,
>
> Yaroslav
>

Re: [flask] Flask with MongoDB

From:
Jörg Bönisch
Date:
2014-09-04 @ 06:06
Yaroslav,

are you used to python modules and packages?
e.g.:
https://docs.python.org/3.4/tutorial/modules.html


For you something like the following in your __init__.py should work:

from config import DevelopmentConfig
...
app.config.from_object(DevelopmentConfig)

or
import config
...
app.config.from_object(config.DevelopmentConfig)

or
... there are plenty ways to do that.

HTH, Jörg

Re: [flask] Flask with MongoDB

From:
Yaroslav Kyrpych
Date:
2014-09-05 @ 23:49
Thank you so much!


On Thu, Sep 4, 2014 at 1:06 AM, Jörg Bönisch <joerg@joergwork.org> wrote:

> Yaroslav,
>
> are you used to python modules and packages?
> e.g.:
> https://docs.python.org/3.4/tutorial/modules.html
>
>
> For you something like the following in your __init__.py should work:
>
> from config import DevelopmentConfig
> ...
> app.config.from_object(DevelopmentConfig)
>
> or
> import config
> ...
> app.config.from_object(config.DevelopmentConfig)
>
> or
> ... there are plenty ways to do that.
>
> HTH, Jörg
>
>