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
fixing for lint
  • Loading branch information
BSd3v committed Sep 17, 2025
commit dfe0ac7f106dd1ea300c36adb3faf922a665b406
15 changes: 6 additions & 9 deletions dash/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
from .base_server import BaseDashServer, RequestAdapter

import importlib
from .base_server import BaseDashServer


request_adapter: RequestAdapter
backend: BaseDashServer


_backend_imports = {
"flask": ("dash.backends._flask", "FlaskDashServer", "FlaskRequestAdapter"),
"fastapi": ("dash.backends._fastapi", "FastAPIDashServer", "FastAPIRequestAdapter"),
"quart": ("dash.backends._quart", "QuartDashServer", "QuartRequestAdapter"),
"flask": ("dash.backends._flask", "FlaskDashServer"),
"fastapi": ("dash.backends._fastapi", "FastAPIDashServer"),
"quart": ("dash.backends._quart", "QuartDashServer"),
}


def get_backend(name: str) -> tuple[BaseDashServer, RequestAdapter]:
module_name, server_class, request_class = _backend_imports[name.lower()]
def get_backend(name: str) -> BaseDashServer:
module_name, server_class = _backend_imports[name.lower()]
try:
module = importlib.import_module(module_name)
server = getattr(module, server_class)
Expand Down Expand Up @@ -74,7 +72,6 @@ def get_server_type(server):

__all__ = [
"get_backend",
"request_adapter",
"backend",
"get_server_type",
]
226 changes: 73 additions & 153 deletions dash/backends/_fastapi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import asyncio
from contextvars import copy_context, ContextVar
from typing import TYPE_CHECKING, Any, Callable, Dict
import sys
Expand All @@ -8,27 +9,41 @@
import inspect
import pkgutil
import time
import traceback
from importlib.util import spec_from_file_location
import json
import os
import re

try:
from fastapi import FastAPI, Request, Response, Body
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
from starlette.responses import Response as StarletteResponse
from starlette.datastructures import MutableHeaders
from starlette.types import ASGIApp, Scope, Receive, Send
import uvicorn
except ImportError:
FastAPI = None
Request = None
Response = None
Body = None
JSONResponse = None
StaticFiles = None
StarletteResponse = None
MutableHeaders = None
ASGIApp = None
Scope = None
Receive = None
Send = None
uvicorn = None

from dash.fingerprint import check_fingerprint
from dash import _validate
from dash.exceptions import PreventUpdate
from .base_server import BaseDashServer, RequestAdapter

from fastapi import FastAPI, Request, Response, Body
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
from starlette.responses import Response as StarletteResponse
from starlette.datastructures import MutableHeaders
from starlette.types import ASGIApp, Scope, Receive, Send
import uvicorn
from ._utils import format_traceback_html

if TYPE_CHECKING: # pragma: no cover - typing only
from dash.dash import Dash
from dash import Dash


_current_request_var = ContextVar("dash_current_request", default=None)
Expand All @@ -49,7 +64,7 @@ def get_current_request() -> Request:
return req


class CurrentRequestMiddleware:
class CurrentRequestMiddleware: # pylint: disable=too-few-public-methods
def __init__(self, app: ASGIApp) -> None: # type: ignore[name-defined]
self.app = app

Expand All @@ -66,23 +81,27 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: #
finally:
reset_current_request(token)


# Internal config helpers (local to this file)
_CONFIG_PATH = os.path.join(os.path.dirname(__file__), "dash_config.json")


def _save_config(config):
with open(_CONFIG_PATH, "w") as f:
with open(_CONFIG_PATH, "w", encoding="utf-8") as f:
json.dump(config, f)


def _load_config():
resp = {"debug": False}
try:
if os.path.exists(_CONFIG_PATH):
with open(_CONFIG_PATH, "r") as f:
with open(_CONFIG_PATH, "r", encoding="utf-8") as f:
resp = json.load(f)
except Exception:
except (json.JSONDecodeError, OSError):
pass # ignore errors
return resp


def _remove_config():
try:
os.remove(_CONFIG_PATH)
Expand Down Expand Up @@ -130,113 +149,9 @@ def register_error_handlers(self):
self.error_handling_mode = "ignore"

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
file_cards = []
pattern = re.compile(r' File "(.+)", line (\d+), in (\w+)')
lines = formatted_tb.split("\n")
current_file = None
card_lines = []

for line in 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
return format_traceback_html(
error, self.error_handling_mode, "FastAPI Debugger", "FastAPI"
)

def register_prune_error_handler(self, _secret, prune_errors):
if prune_errors:
Expand All @@ -253,7 +168,7 @@ async def wrapped(*_args, **_kwargs):
return wrapped

def setup_index(self, dash_app: Dash):
async def index(request: Request):
async def index(_request: Request):
return Response(content=dash_app.index(), media_type="text/html")

# pylint: disable=protected-access
Expand All @@ -270,7 +185,7 @@ def _setup_catchall():
**_load_config(), first_run=False
) # do this to make sure dev tools are enabled

async def catchall(request: Request):
async def catchall(_request: Request):
return Response(content=dash_app.index(), media_type="text/html")

# pylint: disable=protected-access
Expand Down Expand Up @@ -308,11 +223,10 @@ def after_request(self, func: Callable[[], Any] | None):

def run(self, dash_app: Dash, host, port, debug, **kwargs):
frame = inspect.stack()[2]
dev_tools = dash_app._dev_tools # pylint: disable=protected-access
config = dict(
{"debug": debug} if debug else {"debug": False},
**{
f"dev_tools_{k}": v for k, v in dash_app._dev_tools.items()
}, # pylint: disable=protected-access
**{f"dev_tools_{k}": v for k, v in dev_tools.items()},
)
_save_config(config)
if debug:
Expand Down Expand Up @@ -348,22 +262,26 @@ def make_response(
def jsonify(self, obj: Any):
return JSONResponse(content=obj)

def _make_before_middleware(self, func: Callable[[], Any] | None):
def _make_before_middleware(self, _func: Callable[[], Any] | None):
async def middleware(request, call_next):
try:
response = await call_next(request)
return response
except PreventUpdate:
# No content, nothing to update
return Response(status_code=204)
except Exception as e:
except (Exception) as e: # pylint: disable=broad-except
# Handle exceptions based on error_handling_mode
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": "An internal server error occurred."},
content={
"error": "InternalServerError",
"message": "An internal server error occurred.",
},
)

return middleware
Expand Down Expand Up @@ -417,27 +335,25 @@ async def serve(request: Request, package_name: str, fingerprinted_path: str):
dash_app, package_name, fingerprinted_path, request
)

# pylint: disable=protected-access
dash_app._add_url(
"_dash-component-suites/{package_name}/{fingerprinted_path:path}",
serve,
)
name = "_dash-component-suites/{package_name}/{fingerprinted_path:path}"
dash_app._add_url(name, serve) # pylint: disable=protected-access

# pylint: disable=unused-argument
def dispatch(self, dash_app: Dash):
async def _dispatch(request: Request):
# pylint: disable=protected-access
body = await request.json()
g = dash_app._initialize_context(body) # pylint: disable=protected-access
cb_ctx = dash_app._initialize_context(
body
) # pylint: disable=protected-access
func = dash_app._prepare_callback(
g, body
cb_ctx, body
) # pylint: disable=protected-access
args = dash_app._inputs_to_vals(
g.inputs_list + g.states_list
cb_ctx.inputs_list + cb_ctx.states_list
) # pylint: disable=protected-access
ctx = copy_context()
partial_func = dash_app._execute_callback(
func, args, g.outputs_list, g
func, args, cb_ctx.outputs_list, cb_ctx
) # pylint: disable=protected-access
response_data = ctx.run(partial_func)
if inspect.iscoroutine(response_data):
Expand Down Expand Up @@ -494,20 +410,24 @@ def register_callback_api_routes(
sig = inspect.signature(handler)
param_names = list(sig.parameters.keys())

async def view_func(request: Request, body: dict = Body(...)):
# Only pass expected params; ignore extras
kwargs = {
k: v for k, v in body.items() if k in param_names and v is not None
}
if inspect.iscoroutinefunction(handler):
result = await handler(**kwargs)
else:
result = handler(**kwargs)
return JSONResponse(content=result)
def make_view_func(handler, param_names):
async def view_func(_request: Request, body: dict = Body(...)):
kwargs = {
k: v
for k, v in body.items()
if k in param_names and v is not None
}
if inspect.iscoroutinefunction(handler):
result = await handler(**kwargs)
else:
result = handler(**kwargs)
return JSONResponse(content=result)

return view_func

self.server.add_api_route(
route,
view_func,
make_view_func(handler, param_names),
methods=methods,
name=endpoint,
include_in_schema=True,
Expand Down Expand Up @@ -566,5 +486,5 @@ def origin(self):
def path(self):
return self._request.url.path

async def get_json(self): # async method retained
return await self._request.json()
def get_json(self):
return asyncio.run(self._request.json())
Loading
Loading