Dash background callbacks not working with SQLAlchemy - python

I'm using Dash 2.6.1 and trying to get Background Callbacks 2.6.1 to work with SQLAlchemy.
If I add from sqlalchemy import create_engine to the first example of the tutorial then click on the Run Job! button on the webpage I get the stack trace below.
from sqlalchemy import create_engine # This breaks Dash.
import time
import os
import dash
from dash import DiskcacheManager, CeleryManager, Input, Output, html
if 'REDIS_URL' in os.environ:
# Use Redis & Celery if REDIS_URL set as an env variable
from celery import Celery
celery_app = Celery(__name__, broker=os.environ['REDIS_URL'], backend=os.environ['REDIS_URL'])
background_callback_manager = CeleryManager(celery_app)
else:
# Diskcache for non-production apps when developing locally
import diskcache
cache = diskcache.Cache("./cache")
background_callback_manager = DiskcacheManager(cache)
app = dash.Dash(__name__)
app.layout = html.Div(
[
html.Div([html.P(id="paragraph_id", children=["Button not clicked"])]),
html.Button(id="button_id", children="Run Job!"),
]
)
#dash.callback(
output=Output("paragraph_id", "children"),
inputs=Input("button_id", "n_clicks"),
background=True,
manager=background_callback_manager,
)
def update_clicks(n_clicks):
time.sleep(2.0)
return [f"Clicked {n_clicks} times"]
if __name__ == "__main__":
app.run_server(debug=True)
Produces the following when run:
Dash is running on http://127.0.0.1:8050/
* Serving Flask app 'main'
* Debug mode: on
Process Process-3:
Traceback (most recent call last):
File "/Users/one/.pyenv/versions/live/lib/python3.9/site-packages/multiprocess/process.py", line 315, in _bootstrap
self.run()
File "/Users/one/.pyenv/versions/live/lib/python3.9/site-packages/multiprocess/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/Users/one/.pyenv/versions/live/lib/python3.9/site-packages/dash/long_callback/managers/diskcache_manager.py", line 179, in job_fn
cache.set(result_key, user_callback_output)
File "/Users/one/.pyenv/versions/live/lib/python3.9/site-packages/diskcache/core.py", line 796, in set
with self._transact(retry, filename) as (sql, cleanup):
File "/Users/one/.pyenv/versions/3.9.9/lib/python3.9/contextlib.py", line 119, in __enter__
return next(self.gen)
File "/Users/one/.pyenv/versions/live/lib/python3.9/site-packages/diskcache/core.py", line 710, in _transact
sql = self._sql
File "/Users/one/.pyenv/versions/live/lib/python3.9/site-packages/diskcache/core.py", line 648, in _sql
return self._con.execute
File "/Users/one/.pyenv/versions/live/lib/python3.9/site-packages/diskcache/core.py", line 623, in _con
con = self._local.con = sqlite3.connect(
sqlite3.OperationalError: disk I/O error
Is this a bug in Dash? How can I make it work?

Not sure if it works, but I think you can try the following things...
import sqlalchemy
sqlalchemy.create_engine()

Related

Python Dash server with waitress

I have a dashboard application written in Dash framework.
It also has a few Restful API's written using flask.
I am adding flask app to Dash server, as
import dash
import flask
import dash_bootstrap_components as dbc
flask_server = flask.Flask(__name__)
app = dash.Dash(__name__,server=flask_server, external_stylesheets=[dbc.themes.BOOTSTRAP])
And am running the server as
from dashboard import app
from waitress import serve
if __name__ == "__main__":
app.title = 'Litmus'
app.run_server(debug=False)
# serve(app,host="0.0.0.0",port=8050)
The above code works fine when I am using app.run_server(debug=False) but it throws excetion when I use waitress to run the server.
When I use following lines
#app.run_server(debug=False)
serve(app,host="0.0.0.0",port=8050)
I get following error
ERROR:waitress:Exception while serving /
Traceback (most recent call last):
File "C:\Users\litmus\AppData\Roaming\Python\Python38\site-packages\waitress\channel.py", line 397, in service
task.service()
File "C:\Users\litmus\AppData\Roaming\Python\Python38\site-packages\waitress\task.py", line 168, in service
self.execute()
File "C:\Users\litmus\AppData\Roaming\Python\Python38\site-packages\waitress\task.py", line 434, in execute
app_iter = self.channel.server.application(environ, start_response)
TypeError: 'Dash' object is not callable
ERROR:waitress:Exception while serving /favicon.ico
Traceback (most recent call last):
File "C:\Users\litmus\AppData\Roaming\Python\Python38\site-packages\waitress\channel.py", line 397, in service
task.service()
File "C:\Users\litmus\AppData\Roaming\Python\Python38\site-packages\waitress\task.py", line 168, in service
self.execute()
File "C:\Users\litmus\AppData\Roaming\Python\Python38\site-packages\waitress\task.py", line 434, in execute
app_iter = self.channel.server.application(environ, start_response)
TypeError: 'Dash' object is not callable
It's not working because you're passing the Dash app instead of the Flask app to serve.
So instead of this:
serve(app,host="0.0.0.0",port=8050)
pass the Flask app instance like this:
serve(app.server, host="0.0.0.0", port=8050)

RuntimeError: Working outside of application context. in my flask app

I'm trying to schedule sending email from my flask application with this function :
from apscheduler.scheduler import Scheduler
scheduler = Scheduler()
scheduler.start()
def email_job_scheduling():
to="abdellah.ala#gmail.com"
subject="summary projects"
message="your summary projects"
send_email(to,subject,message)
scheduler.add_cron_job(email_job_scheduling, day_of_week='tue', hour=12, minute=55)
this is how i declare app in my file init.py, is there any relationship or must i add schedule function in this file.
login_manager = LoginManager()
db = SQLAlchemy()
mail = Mail()
def create_app(config_name):
app = Flask(__name__, instance_relative_config=True)
app.config.from_object(app_config[config_name])
app.config.from_pyfile('config.py')
app.permanent_session_lifetime = timedelta(minutes=10)
db.init_app(app)
mail.init_app(app)
login_manager.init_app(app)
return app
but I receive this error,
Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to
quit) Job "email_job_scheduling (trigger: cron[day_of_week='wed',
hour='9', minute='57'], next run at: 2019-12-11 09:57:00)" raised an
exception Traceback (most recent call last): File
"/home/abdellah/Documents/venv/lib64/python3.6/site-packages/apscheduler/scheduler.py",
line 512, in _run_job
retval = job.func(*job.args, **job.kwargs) File "/home/abdellah/Documents/SUPPORT-STS/project/app/admin/views.py",
line 29, in email_job_scheduling
send_email(to,subject,message) File "/home/abdellah/Documents/SUPPORT-STS/project/app/emails.py",
line 11, in send_email
mail.send(msg) File "/home/abdellah/Documents/venv/lib64/python3.6/site-packages/flask_mail.py",
line 491, in send
with self.connect() as connection: File "/home/abdellah/Documents/venv/lib64/python3.6/site-packages/flask_mail.py",
line 508, in connect
return Connection(app.extensions['mail']) File "/home/abdellah/Documents/venv/lib64/python3.6/site-packages/werkzeug/local.py",
line 348, in getattr
return getattr(self._get_current_object(), name) File "/home/abdellah/Documents/venv/lib64/python3.6/site-packages/werkzeug/local.py",
line 307, in _get_current_object
return self.__local() File "/home/abdellah/Documents/venv/lib64/python3.6/site-packages/flask/globals.py",
line 52, in _find_app
raise RuntimeError(_app_ctx_err_msg) RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that
needed to interface with the current application object in some way.
To solve this, set up an application context with app.app_context().
See the documentation for more information.
Yes if you have not specified an application context you should create it.
This is because it is necessary to define all the necessary resources within the Flask application.
The documentation explains perfectly in which cases and how you should use a context application in Flask.
https://flask.palletsprojects.com/en/1.0.x/appcontext/
If you post more code I could be more helpful.
I will try to find a solution by the way:
from flask import g
def email_job_scheduling():
a = "abdellah.ala#gmail.com"
subject = "summary projects"
message = "your summary projects"
send_email (to, subject, message)
def get_scheduler():
if "scheduler" is not in g:
g.scheduler = Scheduler()
g.scheduler.start()
returns g.scheduler
with app.app_context(): #add the necessary resources
scheduler = get_scheduler()
#add another code if you need to set another resource
#This piece of code I think is in the main
scheduler.add_cron_job (email_job_scheduling, day_of_week = 'tue', now = 12, minute = 55)

scheduling lambda function in zappa to stop instances

Here I am I created a flask application and deploying using zappa .
While deployment I am facing no module found exception but same python code it working offline below is my stop.app application
import boto3
from flask import Flask, request,Response, jsonify
app = Flask(__name__)
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
region = 'ap-south-1'
myins = ['i-043ae2fbfc26d423f','i-0df3f5ead69c6428c','i-0bac8502574c0cf1d','i-02e866c4c922f1e27']
#app.route('/', methods=['GET'])
def lambda_handler(event=None, context=None):
logger.info('Lambda function invoked index()')
ec2 = boto3.resource('ec2')
ec2client = boto3.client('ec2',region_name=region)
ec2client.stop_instances(InstanceIds=myins)
return 'Instances are stopped!!'
# if __name__ == '__main__':
# app.run(debug=True)```
Below is the error
alling tail for stage dev..
[1568791437587] No module named stop: ImportError
Traceback (most recent call last):
File "/var/task/handler.py", line 602, in lambda_handler
return LambdaHandler.lambda_handler(event, context)
File "/var/task/handler.py", line 245, in lambda_handler
handler = cls()
File "/var/task/handler.py", line 139, in __init__
self.app_module = importlib.import_module(self.settings.APP_MODULE)
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named stop
Worth checking the way the zip file is created, Also check the uploaded zip if its in the correct format.
The lambda_hanlder should be at the base of the root of the zip.
User pip install -r requirements.txt -t .
Also just a small improvement move the ec2 connection outside of lambda_handler.
To start and stop the instance boto3 docs start and stop instance

Flask how to get config element from another file (for example: helper.py)

Good day.
I have one question. How to access a configuration item from another .py file
For example.
app/__init__.py
def create_app(config=None):
app = Flask(__name__, instance_relative_config=True)
app._static_folder = './static'
app.config.from_pyfile('flask.cfg')
app.app_context().push()
return app
wsgi.py
from app import create_app
from werkzeug.debug import DebuggedApplication
if __name__ == "__main__":
app = create_app()
application = DebuggedApplication(app, evalex=True)
app.logger.info("Debug status is: " + str(app.config['DEBUG']))
app.run(use_reloader=True, debug=True)
app/core/helper.py
from flask import current_app as app
def get_dir():
return app.config["PATH_CACHE_DIR"]
Try to call method app/core/cached.py
from flask_caching import Cache
from flask import current_app as app
from app.core.helper import get_dir
print get_dir()
So, i receive the following error
Traceback (most recent call last):
File "/home/stanislav/Python/mbgp/wsgi.py", line 1, in <module>
from app import create_app
File "/home/stanislav/Python/mbgp/app/__init__.py", line 4, in <module>
from app.core.cached import cache
File "/home/stanislav/Python/mbgp/app/core/cached.py", line 5, in <module>
print get_dir()
File "/home/stanislav/Python/mbgp/app/core/helper.py", line 14, in get_dir
return app.config["PATH_CACHE_DIR"]
File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 347, in __getattr__
return getattr(self._get_current_object(), name)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 306, in _get_current_object
return self.__local()
File "/usr/local/lib/python2.7/dist-packages/flask/globals.py", line 51, in _find_app
raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
to interface with the current applicat`enter code here`ion object in some way. To solve
this, set up an application context with app.app_context(). See the
documentation for more information.
Please tell me how to fix this, thank you very much.

Flask-Assets and Flask-Testing throws RegisterError: Another bundle is already registered

I have my Flask app which uses Flask-Assets and while trying to run the unittest cases, except the first testcase, others fails with the following RegisterError.
======================================================================
ERROR: test_login_page (tests.test_auth.AuthTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/cnu/env/flenv/lib/python2.7/site-packages/nose/case.py", line 133, in run
self.runTest(result)
File "/Users/cnu/env/flenv/lib/python2.7/site-packages/nose/case.py", line 151, in runTest
test(result)
File "/Users/cnu/env/flenv/lib/python2.7/site-packages/flask_testing.py", line 72, in __call__
self._pre_setup()
File "/Users/cnu/env/flenv/lib/python2.7/site-packages/flask_testing.py", line 80, in _pre_setup
self.app = self.create_app()
File "/Users/cnu/Projects/Bookworm/App/tests/test_auth.py", line 8, in create_app
return create_app('testing.cfg')
File "/Users/cnu/Projects/Bookworm/App/bookworm/__init__.py", line 118, in create_app
configure_extensions(app)
File "/Users/cnu/Projects/Bookworm/App/bookworm/__init__.py", line 106, in configure_extensions
assets.register('js_all', js)
File "/Users/cnu/env/flenv/src/webassets/src/webassets/env.py", line 374, in register
'as "%s": %s' % (name, self._named_bundles[name]))
RegisterError: Another bundle is already registered as "js_all": <Bundle output=assets/packed.js, filters=[<webassets.filter.jsmin.JSMin object at 0x10fa8af90>], contents=('js/app.js',)>
My understanding of why this happens in before the first testcase is run, create_app creates an instance of app and this is maintained for all other testcases.
I tried del(app) in the teardown method, but doesn't help.
Is there some way to fix it?
You probably have a global object for the assets environment, which you have declared as such :
in file app/extensions.py:
from flask.ext.assets import Environment
assets = Environment()
Then, somewhere in your create_app method, you should init the environment:
in file app/__init__.py:
from .extensions import assets
def create_app():
app = Flask(__name__)
...
assets.init_app(app)
...
return app
The thing is that when you initialize your environment with you app, the registered bundles aren't cleared. So you you should do it manually as such in your TestCase:
in file tests/__init__.py
from app import create_app
from app.extensions import assets
class TestCase(Base):
def create_app(self):
assets._named_bundles = {} # Clear the bundle list
return create_app(self)
Hope this helps,
Cheers

Categories

Resources