OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to store database uri in Flask config for SQLAlchemy?

  • Thread starter Thread starter Peter Kronenberg
  • Start date Start date
P

Peter Kronenberg

Guest
I'm using Flask and SQLAlchemy (not flask-sqlalchemy).

I'm trying to get access to the flask config from my database code, but it's telling me that I'm not in an application context, probably because the app is not yet fully initialized.

I have app.py:

Code:
from flask import Flask

from root.views.calibration_views import calibration

nwm_app = Flask(__name__)
nwm_app.config.from_pyfile('myConfig.cfg')
print('config', nwm_app.config)

My calibration_views file imports database.py, which looks like this:

Code:
from sqlalchemy import URL, create_engine
from sqlalchemy.orm import sessionmaker

from flask import current_app as nwm_app


url = URL.create(
    drivername="postgresql",
    username="postgres",
    password="postgres",
    host="localhost",
    database="flask_test"
)
engine = create_engine(url, echo=True)

with nwm_app.app_context():
    print('config', nwm_app.config)


Session = sessionmaker(bind=engine)

I'm trying to get the config object using current_app, which avoids the Python error about circular imports, but I still get

Code:
RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that was needed.

The current application. To solve this, set up an application context.

With app.app_context(). See the documentation for more information.

The nwm_app.app_context() was my attempt to fix it, but it had no effect

Part of me is wondering if the Flask config should really be used like this, even though they say you are allowed to put your own variables in there. It would be easy enough to just load a separate config file using normally Python behavior.
<p>I'm using Flask and SQLAlchemy (<em>not</em> flask-sqlalchemy).</p>
<p>I'm trying to get access to the flask config from my database code, but it's telling me that I'm not in an application context, probably because the app is not yet fully initialized.</p>
<p>I have <code>app.py</code>:</p>
<pre><code>from flask import Flask

from root.views.calibration_views import calibration

nwm_app = Flask(__name__)
nwm_app.config.from_pyfile('myConfig.cfg')
print('config', nwm_app.config)
</code></pre>
<p>My <code>calibration_views</code> file imports <code>database.py</code>, which looks like this:</p>
<pre><code>from sqlalchemy import URL, create_engine
from sqlalchemy.orm import sessionmaker

from flask import current_app as nwm_app


url = URL.create(
drivername="postgresql",
username="postgres",
password="postgres",
host="localhost",
database="flask_test"
)
engine = create_engine(url, echo=True)

with nwm_app.app_context():
print('config', nwm_app.config)


Session = sessionmaker(bind=engine)
</code></pre>
<p>I'm trying to get the config object using <code>current_app</code>, which avoids the Python error about circular imports, but I still get</p>
<pre><code>RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that was needed.

The current application. To solve this, set up an application context.

With app.app_context(). See the documentation for more information.
</code></pre>
<p>The <code>nwm_app.app_context()</code> was my attempt to fix it, but it had no effect</p>
<p>Part of me is wondering if the Flask config should really be used like this, even though they say you are allowed to put your own variables in there. It would be easy enough to just load a separate config file using normally Python behavior.</p>
 

Latest posts

Online statistics

Members online
0
Guests online
3
Total visitors
3
Ads by Eonads
Top