3333
3434from dash .fingerprint import check_fingerprint
3535from dash import _validate
36- from dash .exceptions import PreventUpdate , InvalidResourceError , InvalidCallbackReturnValue , BackgroundCallbackError
36+ from dash .exceptions import (
37+ PreventUpdate ,
38+ InvalidResourceError ,
39+ InvalidCallbackReturnValue ,
40+ BackgroundCallbackError ,
41+ )
3742from dash .backend import set_request_adapter
3843from .base_server import BaseDashServer
3944
4247
4348CONFIG_PATH = "dash_config.json"
4449
50+
4551def save_config (config ):
4652 with open (CONFIG_PATH , "w" ) as f :
4753 json .dump (config , f )
4854
55+
4956def load_config ():
5057 if os .path .exists (CONFIG_PATH ):
5158 with open (CONFIG_PATH , "r" ) as f :
5259 return json .load (f )
5360 return {}
5461
62+
5563class FastAPIDashServer (BaseDashServer ):
5664 def __init__ (self ):
5765 self .error_handling_mode = "prune"
@@ -96,7 +104,7 @@ def _get_traceback(self, secret, error: Exception):
96104 for err in errors :
97105 if self .error_handling_mode == "prune" :
98106 if not callback_handled :
99- if ' callback invoked' in str (err ) and ' _callback.py' in str (err ):
107+ if " callback invoked" in str (err ) and " _callback.py" in str (err ):
100108 callback_handled = True
101109 continue
102110 pass_errs .append (err )
@@ -106,9 +114,10 @@ def _get_traceback(self, secret, error: Exception):
106114
107115 # Parse traceback lines to group by file
108116 import re
117+
109118 file_cards = []
110119 pattern = re .compile (r' File "(.+)", line (\d+), in (\w+)' )
111- lines = formatted_tb .split (' \n ' )
120+ lines = formatted_tb .split (" \n " )
112121 current_file = None
113122 card_lines = []
114123
@@ -117,7 +126,9 @@ def _get_traceback(self, secret, error: Exception):
117126 if match :
118127 if current_file and card_lines :
119128 file_cards .append ((current_file , card_lines ))
120- current_file = f'{ match .group (1 )} (line { match .group (2 )} , in { match .group (3 )} )'
129+ current_file = (
130+ f"{ match .group (1 )} (line { match .group (2 )} , in { match .group (3 )} )"
131+ )
121132 card_lines = [line ]
122133 elif current_file :
123134 card_lines .append (line )
@@ -126,12 +137,16 @@ def _get_traceback(self, secret, error: Exception):
126137
127138 cards_html = ""
128139 for filename , card in file_cards :
129- cards_html += f"""
140+ cards_html += (
141+ f"""
130142 <div class="error-card">
131143 <div class="error-card-header">{ filename } </div>
132- <pre class="error-card-traceback">""" + '\n ' .join (card ) + """</pre>
144+ <pre class="error-card-traceback">"""
145+ + "\n " .join (card )
146+ + """</pre>
133147 </div>
134148 """
149+ )
135150
136151 html = f"""
137152 <!doctype html>
@@ -198,7 +213,6 @@ def register_prune_error_handler(self, _app, _secret, prune_errors):
198213 else :
199214 self .error_handling_mode = "raise"
200215
201-
202216 def _html_response_wrapper (self , view_func ):
203217 async def wrapped (* _args , ** _kwargs ):
204218 # If view_func is a function, call it; if it's a string, use it directly
@@ -221,9 +235,7 @@ def setup_catchall(self, dash_app):
221235 @dash_app .server .on_event ("startup" )
222236 def _setup_catchall ():
223237 config = load_config ()
224- dash_app .enable_dev_tools (
225- ** config , first_run = False
226- )
238+ dash_app .enable_dev_tools (** config , first_run = False )
227239
228240 async def catchall (request : Request ):
229241 adapter = FastAPIRequestAdapter ()
@@ -260,13 +272,15 @@ def after_request(self, app, func):
260272
261273 def run (self , dash_app , app , host , port , debug , ** kwargs ):
262274 frame = inspect .stack ()[2 ]
263- config = dict ({"debug" : debug } if debug else {}, ** {
264- f'dev_tools_{ k } ' : v for k , v in dash_app ._dev_tools .items ()})
275+ config = dict (
276+ {"debug" : debug } if debug else {},
277+ ** {f"dev_tools_{ k } " : v for k , v in dash_app ._dev_tools .items ()},
278+ )
265279 save_config (config )
266280 if debug :
267- if kwargs .get (' reload' ) is None :
268- kwargs [' reload' ] = True
269- if kwargs .get (' reload' ):
281+ if kwargs .get (" reload" ) is None :
282+ kwargs [" reload" ] = True
283+ if kwargs .get (" reload" ):
270284 # Dynamically determine the module name from the file path
271285 file_path = frame .filename
272286 module_name = importlib .util .spec_from_file_location ("app" , file_path ).name
@@ -305,7 +319,7 @@ async def middleware(request, call_next):
305319 if self .error_handling_mode in ["raise" , "prune" ]:
306320 # Prune the traceback to remove internal Dash calls
307321 tb = self ._get_traceback (None , e )
308- return Response (content = tb , media_type = ' text/html' , status_code = 500 )
322+ return Response (content = tb , media_type = " text/html" , status_code = 500 )
309323 return JSONResponse (
310324 status_code = 500 ,
311325 content = {"error" : "InternalServerError" , "message" : str (e .args [0 ])},
0 commit comments