11from __future__ import annotations
22
3- import asyncio
43from contextvars import copy_context , ContextVar
54import json
65from typing import TYPE_CHECKING , Any , Callable , Dict
1312import os
1413import subprocess
1514import threading
15+ import traceback
1616
1717try :
1818 from fastapi import FastAPI , Request , Response , Body
3232from dash .exceptions import PreventUpdate
3333from .base_server import BaseDashServer , RequestAdapter , ResponseAdapter
3434from ._utils import format_traceback_html
35- import traceback
3635
3736if TYPE_CHECKING : # pragma: no cover - typing only
3837 from dash import Dash
@@ -126,8 +125,14 @@ async def _initialize_dev_tools(self) -> None:
126125 async def _setup_timing (self , request : Request ) -> None :
127126 """Set up timing information for the request."""
128127 try :
129- request .state .json_body = await request .json () if request .headers .get ("content-type" , "" ).startswith ("application/json" ) else None
130- except :
128+ request .state .json_body = (
129+ await request .json ()
130+ if request .headers .get ("content-type" , "" ).startswith (
131+ "application/json"
132+ )
133+ else None
134+ )
135+ except Exception : # pylint: disable=broad-exception-caught
131136 request .state .json_body = None
132137 if self .enable_timing :
133138 request .state .timing_information = {
@@ -187,9 +192,8 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
187192 try :
188193 dash_app = get_app ()
189194 dash_app .backend ._setup_catchall ()
190- except :
191- print ("Error during catch-all setup:" )
192- print (traceback .format_exc ())
195+ except Exception : # pylint: disable=broad-exception-caught
196+ traceback .print_exc ()
193197 await self ._initialize_dev_tools ()
194198 await self .app (scope , receive , send )
195199 return
@@ -286,24 +290,24 @@ async def index(_request: Request):
286290 dash_app ._add_url ("" , index , methods = ["GET" ])
287291
288292 def setup_catchall (self , dash_app : Dash ):
289- ''' This is needed to ensure that all routes are handled by FastAPI
293+ """ This is needed to ensure that all routes are handled by FastAPI
290294 and passed through the middleware, which is necessary for features like authentication
291295 and timing to work correctly on all routes. FastAPI will match this catch-all route
292296 for any path that isn't matched by a more specific route, allowing the middleware to
293- process the request and then return the appropriate response (e.g., 404 if no Dash route matches).'''
294-
297+ process the request and then return the appropriate response (e.g., 404 if no Dash route matches)."""
295298
296299 def _setup_catchall (self ):
297300 try :
298- print ("Setting up catch-all route for unmatched paths" )
301+ print ("Setting up catch-all route for unmatched paths" , file = sys . stderr )
299302 dash_app = get_app ()
303+
300304 async def catchall (_request : Request ):
301305 return Response (content = dash_app .index (), media_type = "text/html" )
302306
303307 # pylint: disable=protected-access
304308 self .add_url_rule ("{path:path}" , catchall , methods = ["GET" ])
305- except :
306- print ( traceback .format_exc () )
309+ except Exception : # pylint: disable=broad-exception-caught
310+ traceback .print_exc ( )
307311
308312 def add_url_rule (
309313 self ,
@@ -313,7 +317,10 @@ def add_url_rule(
313317 methods : list [str ] | None = None ,
314318 include_in_schema : bool = False ,
315319 ):
316- print (f"Adding URL rule: { rule } -> { view_func } (endpoint: { endpoint } , methods: { methods } )" )
320+ print (
321+ f"Adding URL rule: { rule } -> { view_func } (endpoint: { endpoint } , methods: { methods } )" ,
322+ file = sys .stderr ,
323+ )
317324 if rule == "" :
318325 rule = "/"
319326 if isinstance (view_func , str ):
@@ -504,7 +511,7 @@ def add_redirect_rule(self, app, fullname, path):
504511 )
505512
506513 def serve_callback (self , dash_app : Dash ):
507- async def _dispatch (request : Request ):
514+ async def _dispatch (request : Request ): # pylint: disable=unused-argument
508515 # pylint: disable=protected-access
509516 body = self .request_adapter ().get_json ()
510517 cb_ctx = dash_app ._initialize_context (
@@ -666,7 +673,7 @@ def origin(self):
666673 def path (self ):
667674 return self ._request .url .path
668675
669- async def _get_json (self , request : Request = None ):
676+ async def _get_json (self , request : Request = None ):
670677 req = self ._request
671678 if not hasattr (req .state , "json_body" ):
672679 req .state .json_body = await request .json ()
0 commit comments