【问题标题】:Dash background callbacks not working with SQLAlchemyDash 后台回调不适用于 SQLAlchemy
【发布时间】:2023-01-30 00:19:25
【问题描述】:

我正在使用 Dash 2.6.1 并试图让 Background Callbacks 2.6.1 与 SQLAlchemy 一起工作。

如果我将 from sqlalchemy import create_engine 添加到教程的第一个示例,然后单击网页上的 Run Job! 按钮,我将获得下面的堆栈跟踪。

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)

运行时产生以下内容:

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

这是 Dash 中的错误吗?我怎样才能让它发挥作用?

【问题讨论】:

    标签: python sqlalchemy plotly-dash


    【解决方案1】:

    不确定它是否有效,但我认为你可以尝试以下操作......

    import sqlalchemy
    sqlalchemy.create_engine()
    

    【讨论】:

      猜你喜欢
      • 2020-06-01
      • 1970-01-01
      • 2019-10-10
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多