Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
c457ab7
initial push
BSd3v Sep 9, 2025
4ebc657
work to modularize the dash eco-system and decouple from Flask
BSd3v Sep 9, 2025
9dff791
fix favicon
BSd3v Sep 9, 2025
c319b18
removing changelog entry
BSd3v Sep 9, 2025
7de2a41
fixing issue with debug true for FastAPI
BSd3v Sep 9, 2025
2cd769e
fixing `catchall` for path routes
BSd3v Sep 9, 2025
686f32f
fixing pages for use with `fastapi`
BSd3v Sep 9, 2025
660e257
fixing issue with flask pages
BSd3v Sep 10, 2025
581f8a5
Merge pull request #3 from BSd3v/modularize-dash-server
BSd3v Sep 10, 2025
9eb9dd0
Merge branch 'dev' into bring-your-own-server
BSd3v Sep 10, 2025
0fa5c99
fixing for lint
BSd3v Sep 11, 2025
1088331
fixing issue with failing test due to `endpoint` name
BSd3v Sep 11, 2025
4920e33
fixing `run` command to trigger `devtools` properly
BSd3v Sep 11, 2025
9ffba5a
fixing issue with lint and debug ui
BSd3v Sep 11, 2025
908aacd
fixing issue with `_app` when using dispatch, need to keep in context
BSd3v Sep 11, 2025
9491c7f
fixing issue with catchall
BSd3v Sep 11, 2025
39ad7bd
fixing issue with args and cancelling callbacks
BSd3v Sep 11, 2025
7bf69a7
fixing issues with pages metadata and flaky tests
BSd3v Sep 11, 2025
10681dc
fixing issues with relativate paths
BSd3v Sep 11, 2025
4944d6d
∙ - initial quart factory
Sep 11, 2025
3b0f47e
Quart factory ready
Sep 12, 2025
1112f77
fixing for lint
BSd3v Sep 12, 2025
8c52bbb
fixing issue with apps overwriting other paths
BSd3v Sep 12, 2025
aabeeb7
removing print
BSd3v Sep 12, 2025
5659cd7
cleanup
Sep 12, 2025
b05e376
reverting `render_index` -> `index` and making catch for outside of a…
BSd3v Sep 12, 2025
ed0dc3b
∙ - initial quart factory
Sep 11, 2025
141527c
Quart factory ready
Sep 12, 2025
3e38d41
fixing `prune_errors` test
BSd3v Sep 12, 2025
381fb0c
adjustments for flask api_endpoint declared in callback defs
BSd3v Sep 12, 2025
a27927a
updated QuartRequestAdapter & QuartFactory to latest changes
Sep 12, 2025
fbc3935
checkout
Sep 12, 2025
1824e11
Removed redundant Response return
Sep 12, 2025
b14f6d2
fix for fastapi `api_endpoint` registering
BSd3v Sep 12, 2025
fbefbc9
Merge pull request #4 from chgiesse/quart-factory
BSd3v Sep 12, 2025
5ef796b
shifting from `server_factory` to `backend`
BSd3v Sep 12, 2025
a4ca566
adding missing files
BSd3v Sep 12, 2025
708773f
fixing issue with server not declared
BSd3v Sep 12, 2025
b7bceba
Update dash/dash.py
BSd3v Sep 12, 2025
9873079
Update dash/dash.py
BSd3v Sep 12, 2025
9f4d291
Update dash/backend/quart.py
BSd3v Sep 12, 2025
da86e86
Update dash/dash.py
BSd3v Sep 12, 2025
4c60740
Update dash/dash.py
BSd3v Sep 12, 2025
84cb5e5
update for caller_name
BSd3v Sep 12, 2025
29cf823
Update dash/dash.py
BSd3v Sep 12, 2025
5d0f4dc
Update dash/dash.py
BSd3v Sep 12, 2025
86f4528
adjustments for matching types
BSd3v Sep 12, 2025
2a88385
Update dash/backend/registry.py
BSd3v Sep 12, 2025
df76ed6
Merge branch 'factory-backend' of github.com:BSd3v/dash into factory-…
BSd3v Sep 12, 2025
bc51c0d
Update dash/backend/registry.py
BSd3v Sep 12, 2025
1b4d0d3
fixing another type check
BSd3v Sep 12, 2025
f867f98
fixing for lint
BSd3v Sep 13, 2025
0ed81ce
fixing failing test
BSd3v Sep 13, 2025
6bd342a
fixing issue with fastapi and component suites
BSd3v Sep 13, 2025
b1c9953
adjustments to fix issues with caller_name and init the app a couple …
BSd3v Sep 13, 2025
bd40b56
adjustments for failing tests
BSd3v Sep 13, 2025
4e50430
format dash
BSd3v Sep 13, 2025
0d32e65
removing `FlaskDashServer` from import and using `get_backend('flask'…
BSd3v Sep 14, 2025
1b3f61e
reverting change to callable(title) process
BSd3v Sep 14, 2025
c6805b5
fixing for lint
BSd3v Sep 14, 2025
8c78089
adding custom error handling per backend, tests and adjustments to th…
BSd3v Sep 16, 2025
5211f6f
adjusments for formatting
BSd3v Sep 16, 2025
6a34208
adjustment to retest backend
BSd3v Sep 16, 2025
1a2b531
adding missing reqs association
BSd3v Sep 16, 2025
465e45e
fixing minor linting issues
BSd3v Sep 16, 2025
c43a583
Add global Request Adapter (#6)
chgiesse Sep 17, 2025
c4795ed
fixes for failing tests
BSd3v Sep 17, 2025
567d0f8
fixing formatting
BSd3v Sep 17, 2025
a855c6d
fixing issues
BSd3v Sep 17, 2025
79afb0b
fixing async validation
BSd3v Sep 17, 2025
77e22a3
adjustments for request_adapter
BSd3v Sep 17, 2025
f7331d3
adding test for custom dash server
BSd3v Sep 17, 2025
8b58cf4
fixing issue with `request_adapter`
BSd3v Sep 17, 2025
b7d4af2
adjusting error handling for fastapi
BSd3v Sep 17, 2025
4cf4686
adjustments for handling issues with `debug` for `fastapi`
BSd3v Sep 17, 2025
dfe0ac7
fixing for lint
BSd3v Sep 17, 2025
cd02cc5
adjustment for delayed config
BSd3v Sep 18, 2025
16b3c9e
fix typing error
BSd3v Sep 18, 2025
493d150
fixes for pages
BSd3v Sep 22, 2025
26f01b2
Merge branch 'dev' into bring-your-own-server
BSd3v Oct 29, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
adding custom error handling per backend, tests and adjustments to th…
…e flow. Made endpoints for downloading the reqs
  • Loading branch information
BSd3v committed Sep 16, 2025
commit 8c7808962c3c7914ab82df2ea74d5691704a35c7
103 changes: 103 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,109 @@ jobs:
cd bgtests
pytest --headless --nopercyfinalize tests/async_tests -v -s

backend-tests:
name: Run Backend Callback Tests (Python ${{ matrix.python-version }})
needs: [build, changes_filter]
if: |
(github.event_name == 'push' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/dev')) ||
needs.changes_filter.outputs.backend_tests_changed == 'true'
timeout-minutes: 30
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.12"]

services:
redis:
image: redis:6
ports:
- 6379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5

env:
REDIS_URL: redis://localhost:6379
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'

- name: Install Node.js dependencies
run: npm ci

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'

- name: Download built Dash packages
uses: actions/download-artifact@v4
with:
name: dash-packages
path: packages/

- name: Install Dash packages
run: |
python -m pip install --upgrade pip wheel
python -m pip install "setuptools<78.0.0"
python -m pip install "selenium==4.32.0"
find packages -name dash-*.whl -print -exec sh -c 'pip install "{}[async,ci,testing,dev,celery,diskcache, fastapi, quart]"' \;

- name: Install Google Chrome
run: |
sudo apt-get update
sudo apt-get install -y google-chrome-stable

- name: Install ChromeDriver
run: |
echo "Determining Chrome version..."
CHROME_BROWSER_VERSION=$(google-chrome --version)
echo "Installed Chrome Browser version: $CHROME_BROWSER_VERSION"
CHROME_MAJOR_VERSION=$(echo "$CHROME_BROWSER_VERSION" | cut -f 3 -d ' ' | cut -f 1 -d '.')
echo "Detected Chrome Major version: $CHROME_MAJOR_VERSION"
if [ "$CHROME_MAJOR_VERSION" -ge 115 ]; then
echo "Fetching ChromeDriver version for Chrome $CHROME_MAJOR_VERSION using CfT endpoint..."
CHROMEDRIVER_VERSION_STRING=$(curl -sS "https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_${CHROME_MAJOR_VERSION}")
if [ -z "$CHROMEDRIVER_VERSION_STRING" ]; then
echo "Could not automatically find ChromeDriver version for Chrome $CHROME_MAJOR_VERSION via LATEST_RELEASE. Please check CfT endpoints."
exit 1
fi
CHROMEDRIVER_URL="https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/${CHROMEDRIVER_VERSION_STRING}/linux64/chromedriver-linux64.zip"
else
echo "Fetching ChromeDriver version for Chrome $CHROME_MAJOR_VERSION using older method..."
CHROMEDRIVER_VERSION_STRING=$(curl -sS "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_${CHROME_MAJOR_VERSION}")
CHROMEDRIVER_URL="https://chromedriver.storage.googleapis.com/${CHROMEDRIVER_VERSION_STRING}/chromedriver_linux64.zip"
fi
echo "Using ChromeDriver version string: $CHROMEDRIVER_VERSION_STRING"
echo "Downloading ChromeDriver from: $CHROMEDRIVER_URL"
wget -q -O chromedriver.zip "$CHROMEDRIVER_URL"
unzip -o chromedriver.zip -d /tmp/
sudo mv /tmp/chromedriver-linux64/chromedriver /usr/local/bin/chromedriver || sudo mv /tmp/chromedriver /usr/local/bin/chromedriver
sudo chmod +x /usr/local/bin/chromedriver
echo "/usr/local/bin" >> $GITHUB_PATH
shell: bash

- name: Build/Setup test components
run: npm run setup-tests.py

- name: Run Backend Callback Tests
run: |
mkdir bgtests
cp -r tests bgtests/tests
cd bgtests
touch __init__.py
pytest --headless --nopercyfinalize tests/backend_tests -v -s

table-unit:
name: Table Unit/Lint Tests (Python ${{ matrix.python-version }})
needs: [build, changes_filter]
Expand Down
184 changes: 156 additions & 28 deletions dash/backend/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from contextvars import copy_context
import importlib.util
import time
import traceback

try:
import uvicorn
Expand All @@ -32,14 +33,28 @@

from dash.fingerprint import check_fingerprint
from dash import _validate
from dash.exceptions import PreventUpdate, InvalidResourceError
from dash.exceptions import PreventUpdate, InvalidResourceError, InvalidCallbackReturnValue, BackgroundCallbackError
from dash.backend import set_request_adapter
from .base_server import BaseDashServer

import json
import os

CONFIG_PATH = "dash_config.json"

def save_config(config):
with open(CONFIG_PATH, "w") as f:
json.dump(config, f)

def load_config():
if os.path.exists(CONFIG_PATH):
with open(CONFIG_PATH, "r") as f:
return json.load(f)
return {}

class FastAPIDashServer(BaseDashServer):
def __init__(self):
self.config = {}
self.error_handling_mode = "prune"
super().__init__()

def __call__(self, server, *args, **kwargs):
Expand Down Expand Up @@ -69,19 +84,120 @@ def register_assets_blueprint(
pass

def register_error_handlers(self, app):
@app.exception_handler(PreventUpdate)
async def _handle_error(_request, _exc):
return Response(status_code=204)
self.error_handling_mode = "prune"
# FastAPI uses exception handlers, but we will handle errors in middleware
pass

def _get_traceback(self, secret, error: Exception):
tb = error.__traceback__
errors = traceback.format_exception(type(error), error, tb)
pass_errs = []
callback_handled = False
for err in errors:
if self.error_handling_mode == "prune":
if not callback_handled:
if 'callback invoked' in str(err) and '_callback.py' in str(err):
callback_handled = True
continue
pass_errs.append(err)
formatted_tb = "".join(pass_errs)
error_type = type(error).__name__
error_msg = str(error)

# Parse traceback lines to group by file
import re
file_cards = []
pattern = re.compile(r' File "(.+)", line (\d+), in (\w+)')
lines = formatted_tb.split('\n')
current_file = None
card_lines = []

for i, line in enumerate(lines[:-1]): # Skip the last line (error message)
match = pattern.match(line)
if match:
if current_file and card_lines:
file_cards.append((current_file, card_lines))
current_file = f'{match.group(1)} (line {match.group(2)}, in {match.group(3)})'
card_lines = [line]
elif current_file:
card_lines.append(line)
if current_file and card_lines:
file_cards.append((current_file, card_lines))

cards_html = ""
for filename, card in file_cards:
cards_html += f"""
<div class="error-card">
<div class="error-card-header">{filename}</div>
<pre class="error-card-traceback">"""+ '\n'.join(card) + """</pre>
</div>
"""

html = f"""
<!doctype html>
<html lang="en">
<head>
<title>{error_type}: {error_msg} // FastAPI Debugger</title>
<style>
body {{ font-family: monospace; background: #fff; color: #333; }}
.debugger {{ margin: 2em; max-width: 700px; }}
.error-card {{
border: 1px solid #ccc;
border-radius: 6px;
margin-bottom: 1em;
padding: 1em;
background: #f9f9f9;
box-shadow: 0 2px 4px rgba(0,0,0,0.03);
overflow: auto;
}}
.error-card-header {{
font-weight: bold;
margin-bottom: 0.5em;
color: #0074d9;
}}
.error-card-traceback {{
max-height: 150px;
overflow: auto;
margin: 0;
white-space: pre-wrap;
}}
.plain textarea {{ width: 100%; height: 10em; resize: vertical; overflow: auto; }}
h1 {{ color: #c00; }}
</style>
</head>
<body style="padding-bottom:10px">
<div class="debugger">
<h1>{error_type}</h1>
<div class="detail">
<p class="errormsg">{error_type}: {error_msg}</p>
</div>
<h2 class="traceback">Traceback <em>(most recent call last)</em></h2>
{cards_html}
<blockquote>{error_type}: {error_msg}</blockquote>
<div class="plain">
<p>This is the Copy/Paste friendly version of the traceback.</p>
<textarea readonly>{formatted_tb}</textarea>
</div>
<div class="explanation">
The debugger caught an exception in your ASGI application. You can now
look at the traceback which led to the error.
</div>
<div class="footer">
Brought to you by <strong class="arthur">DON'T PANIC</strong>, your
friendly FastAPI powered traceback interpreter.
</div>
</div>
</body>
</html>
"""
return html

@app.exception_handler(InvalidResourceError)
async def _invalid_resources_handler(_request, exc):
return Response(content=exc.args[0], status_code=404)
def register_prune_error_handler(self, _app, _secret, prune_errors):
if prune_errors:
self.error_handling_mode = "prune"
else:
self.error_handling_mode = "raise"

def register_prune_error_handler(self, app, secret, get_traceback_func):
@app.exception_handler(Exception)
async def _wrap_errors(_error_request, error):
tb = get_traceback_func(secret, error)
return PlainTextResponse(tb, status_code=500)

def _html_response_wrapper(self, view_func):
async def wrapped(*_args, **_kwargs):
Expand All @@ -104,9 +220,10 @@ async def index(request: Request):
def setup_catchall(self, dash_app):
@dash_app.server.on_event("startup")
def _setup_catchall():
config = load_config()
dash_app.enable_dev_tools(
**self.config, first_run=False
) # do this to make sure dev tools are enabled
**config, first_run=False
)

async def catchall(request: Request):
adapter = FastAPIRequestAdapter()
Expand Down Expand Up @@ -141,23 +258,26 @@ def after_request(self, app, func):
# FastAPI does not have after_request, but we can use middleware
app.middleware("http")(self._make_after_middleware(func))

def run(self, app, host, port, debug, **kwargs):
def run(self, dash_app, app, host, port, debug, **kwargs):
frame = inspect.stack()[2]
self.config = dict({"debug": debug} if debug else {}, **kwargs)
reload = debug
if reload:
config = dict({"debug": debug} if debug else {}, **{
f'dev_tools_{k}': v for k, v in dash_app._dev_tools.items()})
save_config(config)
if debug:
if kwargs.get('reload') is None:
kwargs['reload'] = True
if kwargs.get('reload'):
# Dynamically determine the module name from the file path
file_path = frame.filename
module_name = importlib.util.spec_from_file_location("app", file_path).name
uvicorn.run(
f"{module_name}:app.server",
host=host,
port=port,
reload=reload,
**kwargs,
)
else:
uvicorn.run(app, host=host, port=port, reload=reload, **kwargs)
uvicorn.run(app, host=host, port=port, **kwargs)

def make_response(self, data, mimetype=None, content_type=None):
headers = {}
Expand All @@ -175,13 +295,21 @@ def get_request_adapter(self):

def _make_before_middleware(self, func):
async def middleware(request, call_next):
if func is not None:
if inspect.iscoroutinefunction(func):
await func()
else:
func()
response = await call_next(request)
return response
try:
response = await call_next(request)
return response
except PreventUpdate:
# No content, nothing to update
return Response(status_code=204)
except Exception as e:
if self.error_handling_mode in ["raise", "prune"]:
# Prune the traceback to remove internal Dash calls
tb = self._get_traceback(None, e)
return Response(content=tb, media_type='text/html', status_code=500)
return JSONResponse(
status_code=500,
content={"error": "InternalServerError", "message": str(e.args[0])},
)

return middleware

Expand Down
Loading