A custom Response class that wraps FastAPI's JSONResponse and provides a set_response() method for compatibility with Dash's callback system.
| 57 | |
| 58 | |
| 59 | class FastAPIResponseAdapter(ResponseAdapter): |
| 60 | """ |
| 61 | A custom Response class that wraps FastAPI's JSONResponse |
| 62 | and provides a set_response() method for compatibility with Dash's callback system. |
| 63 | """ |
| 64 | |
| 65 | @property |
| 66 | def callback_response(self): |
| 67 | """Get the response object to be returned from a callback.""" |
| 68 | print( |
| 69 | "Cannot access callback_response directly on FastAPIResponseAdapter. Use set_response() to create a response with data." |
| 70 | ) |
| 71 | raise NotImplementedError() |
| 72 | |
| 73 | def set_response(self, **kwargs): |
| 74 | """ |
| 75 | Set the response data. This method provides compatibility with Flask's Response.set_data(). |
| 76 | """ |
| 77 | data = kwargs.get("data") |
| 78 | if isinstance(data, (str, bytes, bytearray)): |
| 79 | resp = Response(content=data) |
| 80 | else: |
| 81 | resp = JSONResponse(content=data) |
| 82 | if self._headers: |
| 83 | for key, value in self._headers.items(): |
| 84 | if isinstance(value, list): |
| 85 | for v in value: |
| 86 | resp.headers.append(key, v) |
| 87 | else: |
| 88 | resp.headers[key] = value |
| 89 | if self._cookies: |
| 90 | for key, (value, cookie_kwargs) in self._cookies.items(): |
| 91 | resp.set_cookie(key, value, **cookie_kwargs) |
| 92 | return resp |
| 93 | |
| 94 | |
| 95 | _current_request_var = ContextVar("dash_current_request", default=None) |
no outgoing calls
no test coverage detected
searching dependent graphs…