app.py 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. """The main Reflex app."""
  2. from __future__ import annotations
  3. import asyncio
  4. import concurrent.futures
  5. import contextlib
  6. import copy
  7. import functools
  8. import inspect
  9. import io
  10. import multiprocessing
  11. import os
  12. import platform
  13. import sys
  14. import traceback
  15. from datetime import datetime
  16. from pathlib import Path
  17. from typing import (
  18. Any,
  19. AsyncIterator,
  20. Callable,
  21. Coroutine,
  22. Dict,
  23. List,
  24. Optional,
  25. Set,
  26. Type,
  27. Union,
  28. get_args,
  29. get_type_hints,
  30. )
  31. from fastapi import FastAPI, HTTPException, Request, UploadFile
  32. from fastapi.middleware import cors
  33. from fastapi.responses import StreamingResponse
  34. from fastapi.staticfiles import StaticFiles
  35. from rich.progress import MofNCompleteColumn, Progress, TimeElapsedColumn
  36. from socketio import ASGIApp, AsyncNamespace, AsyncServer
  37. from starlette_admin.contrib.sqla.admin import Admin
  38. from starlette_admin.contrib.sqla.view import ModelView
  39. from reflex import constants
  40. from reflex.admin import AdminDash
  41. from reflex.app_mixins import AppMixin, LifespanMixin, MiddlewareMixin
  42. from reflex.base import Base
  43. from reflex.compiler import compiler
  44. from reflex.compiler import utils as compiler_utils
  45. from reflex.compiler.compiler import ExecutorSafeFunctions
  46. from reflex.components.base.app_wrap import AppWrap
  47. from reflex.components.base.error_boundary import ErrorBoundary
  48. from reflex.components.base.fragment import Fragment
  49. from reflex.components.component import (
  50. Component,
  51. ComponentStyle,
  52. evaluate_style_namespaces,
  53. )
  54. from reflex.components.core.banner import connection_pulser, connection_toaster
  55. from reflex.components.core.breakpoints import set_breakpoints
  56. from reflex.components.core.client_side_routing import (
  57. Default404Page,
  58. wait_for_client_redirect,
  59. )
  60. from reflex.components.core.upload import Upload, get_upload_dir
  61. from reflex.components.radix import themes
  62. from reflex.config import get_config
  63. from reflex.event import Event, EventHandler, EventSpec, window_alert
  64. from reflex.model import Model
  65. from reflex.page import (
  66. DECORATED_PAGES,
  67. )
  68. from reflex.route import (
  69. get_route_args,
  70. replace_brackets_with_keywords,
  71. verify_route_validity,
  72. )
  73. from reflex.state import (
  74. BaseState,
  75. RouterData,
  76. State,
  77. StateManager,
  78. StateUpdate,
  79. _substate_key,
  80. code_uses_state_contexts,
  81. )
  82. from reflex.utils import codespaces, console, exceptions, format, prerequisites, types
  83. from reflex.utils.exec import is_prod_mode, is_testing_env, should_skip_compile
  84. from reflex.utils.imports import ImportVar
  85. # Define custom types.
  86. ComponentCallable = Callable[[], Component]
  87. Reducer = Callable[[Event], Coroutine[Any, Any, StateUpdate]]
  88. def default_frontend_exception_handler(exception: Exception) -> None:
  89. """Default frontend exception handler function.
  90. Args:
  91. exception: The exception.
  92. """
  93. console.error(f"[Reflex Frontend Exception]\n {exception}\n")
  94. def default_backend_exception_handler(exception: Exception) -> EventSpec:
  95. """Default backend exception handler function.
  96. Args:
  97. exception: The exception.
  98. Returns:
  99. EventSpec: The window alert event.
  100. """
  101. from reflex.components.sonner.toast import Toaster, toast
  102. error = traceback.format_exc()
  103. console.error(f"[Reflex Backend Exception]\n {error}\n")
  104. error_message = (
  105. ["Contact the website administrator."]
  106. if is_prod_mode()
  107. else [f"{type(exception).__name__}: {exception}.", "See logs for details."]
  108. )
  109. if Toaster.is_used:
  110. return toast(
  111. "An error occurred.",
  112. level="error",
  113. description="<br/>".join(error_message),
  114. position="top-center",
  115. id="backend_error",
  116. style={"width": "500px"},
  117. ) # type: ignore
  118. else:
  119. error_message.insert(0, "An error occurred.")
  120. return window_alert("\n".join(error_message))
  121. def default_overlay_component() -> Component:
  122. """Default overlay_component attribute for App.
  123. Returns:
  124. The default overlay_component, which is a connection_modal.
  125. """
  126. return Fragment.create(
  127. connection_pulser(),
  128. connection_toaster(),
  129. *codespaces.codespaces_auto_redirect(),
  130. )
  131. def default_error_boundary(*children: Component) -> Component:
  132. """Default error_boundary attribute for App.
  133. Args:
  134. *children: The children to render in the error boundary.
  135. Returns:
  136. The default error_boundary, which is an ErrorBoundary.
  137. """
  138. return ErrorBoundary.create(*children)
  139. class OverlayFragment(Fragment):
  140. """Alias for Fragment, used to wrap the overlay_component."""
  141. pass
  142. class App(MiddlewareMixin, LifespanMixin, Base):
  143. """The main Reflex app that encapsulates the backend and frontend.
  144. Every Reflex app needs an app defined in its main module.
  145. ```python
  146. # app.py
  147. import reflex as rx
  148. # Define state and pages
  149. ...
  150. app = rx.App(
  151. # Set global level style.
  152. style={...},
  153. # Set the top level theme.
  154. theme=rx.theme(accent_color="blue"),
  155. )
  156. ```
  157. """
  158. # The global [theme](https://reflex.dev/docs/styling/theming/#theme) for the entire app.
  159. theme: Optional[Component] = themes.theme(accent_color="blue")
  160. # The [global style](https://reflex.dev/docs/styling/overview/#global-styles}) for the app.
  161. style: ComponentStyle = {}
  162. # A list of URLs to [stylesheets](https://reflex.dev/docs/styling/custom-stylesheets/) to include in the app.
  163. stylesheets: List[str] = []
  164. # A component that is present on every page (defaults to the Connection Error banner).
  165. overlay_component: Optional[Union[Component, ComponentCallable]] = (
  166. default_overlay_component()
  167. )
  168. # Error boundary component to wrap the app with.
  169. error_boundary: Optional[ComponentCallable] = default_error_boundary
  170. # Components to add to the head of every page.
  171. head_components: List[Component] = []
  172. # The Socket.IO AsyncServer instance.
  173. sio: Optional[AsyncServer] = None
  174. # The language to add to the html root tag of every page.
  175. html_lang: Optional[str] = None
  176. # Attributes to add to the html root tag of every page.
  177. html_custom_attrs: Optional[Dict[str, str]] = None
  178. # A map from a page route to the component to render. Users should use `add_page`. PRIVATE.
  179. pages: Dict[str, Component] = {}
  180. # The backend API object. PRIVATE.
  181. api: FastAPI = None # type: ignore
  182. # The state class to use for the app. PRIVATE.
  183. state: Optional[Type[BaseState]] = None
  184. # Class to manage many client states.
  185. _state_manager: Optional[StateManager] = None
  186. # Mapping from a route to event handlers to trigger when the page loads. PRIVATE.
  187. load_events: Dict[str, List[Union[EventHandler, EventSpec]]] = {}
  188. # Admin dashboard to view and manage the database. PRIVATE.
  189. admin_dash: Optional[AdminDash] = None
  190. # The async server name space. PRIVATE.
  191. event_namespace: Optional[EventNamespace] = None
  192. # Background tasks that are currently running. PRIVATE.
  193. background_tasks: Set[asyncio.Task] = set()
  194. # Frontend Error Handler Function
  195. frontend_exception_handler: Callable[[Exception], None] = (
  196. default_frontend_exception_handler
  197. )
  198. # Backend Error Handler Function
  199. backend_exception_handler: Callable[
  200. [Exception], Union[EventSpec, List[EventSpec], None]
  201. ] = default_backend_exception_handler
  202. def __init__(self, **kwargs):
  203. """Initialize the app.
  204. Args:
  205. **kwargs: Kwargs to initialize the app with.
  206. Raises:
  207. ValueError: If the event namespace is not provided in the config.
  208. Also, if there are multiple client subclasses of rx.BaseState(Subclasses of rx.BaseState should consist
  209. of the DefaultState and the client app state).
  210. """
  211. if "connect_error_component" in kwargs:
  212. raise ValueError(
  213. "`connect_error_component` is deprecated, use `overlay_component` instead"
  214. )
  215. super().__init__(**kwargs)
  216. base_state_subclasses = BaseState.__subclasses__()
  217. # Special case to allow test cases have multiple subclasses of rx.BaseState.
  218. if not is_testing_env() and len(base_state_subclasses) > 1:
  219. # Only one Base State class is allowed.
  220. raise ValueError(
  221. "rx.BaseState cannot be subclassed multiple times. use rx.State instead"
  222. )
  223. if "breakpoints" in self.style:
  224. set_breakpoints(self.style.pop("breakpoints"))
  225. # Set up the API.
  226. self.api = FastAPI(lifespan=self._run_lifespan_tasks)
  227. self._add_cors()
  228. self._add_default_endpoints()
  229. for clz in App.__mro__:
  230. if clz == App:
  231. continue
  232. if issubclass(clz, AppMixin):
  233. clz._init_mixin(self)
  234. self._setup_state()
  235. # Set up the admin dash.
  236. self._setup_admin_dash()
  237. if sys.platform == "win32" and not is_prod_mode():
  238. # Hack to fix Windows hot reload issue.
  239. from reflex.utils.compat import windows_hot_reload_lifespan_hack
  240. self.register_lifespan_task(windows_hot_reload_lifespan_hack)
  241. def _enable_state(self) -> None:
  242. """Enable state for the app."""
  243. if not self.state:
  244. self.state = State
  245. self._setup_state()
  246. def _setup_state(self) -> None:
  247. """Set up the state for the app.
  248. Raises:
  249. RuntimeError: If the socket server is invalid.
  250. """
  251. if not self.state:
  252. return
  253. config = get_config()
  254. # Set up the state manager.
  255. self._state_manager = StateManager.create(state=self.state)
  256. # Set up the Socket.IO AsyncServer.
  257. if not self.sio:
  258. self.sio = AsyncServer(
  259. async_mode="asgi",
  260. cors_allowed_origins=(
  261. "*"
  262. if config.cors_allowed_origins == ["*"]
  263. else config.cors_allowed_origins
  264. ),
  265. cors_credentials=True,
  266. max_http_buffer_size=constants.POLLING_MAX_HTTP_BUFFER_SIZE,
  267. ping_interval=constants.Ping.INTERVAL,
  268. ping_timeout=constants.Ping.TIMEOUT,
  269. )
  270. elif getattr(self.sio, "async_mode", "") != "asgi":
  271. raise RuntimeError(
  272. f"Custom `sio` must use `async_mode='asgi'`, not '{self.sio.async_mode}'."
  273. )
  274. # Create the socket app. Note event endpoint constant replaces the default 'socket.io' path.
  275. socket_app = ASGIApp(self.sio, socketio_path="")
  276. namespace = config.get_event_namespace()
  277. # Create the event namespace and attach the main app. Not related to any paths.
  278. self.event_namespace = EventNamespace(namespace, self)
  279. # Register the event namespace with the socket.
  280. self.sio.register_namespace(self.event_namespace)
  281. # Mount the socket app with the API.
  282. self.api.mount(str(constants.Endpoint.EVENT), socket_app)
  283. # Check the exception handlers
  284. self._validate_exception_handlers()
  285. def __repr__(self) -> str:
  286. """Get the string representation of the app.
  287. Returns:
  288. The string representation of the app.
  289. """
  290. return f"<App state={self.state.__name__ if self.state else None}>"
  291. def __call__(self) -> FastAPI:
  292. """Run the backend api instance.
  293. Returns:
  294. The backend api.
  295. """
  296. return self.api
  297. def _add_default_endpoints(self):
  298. """Add default api endpoints (ping)."""
  299. # To test the server.
  300. self.api.get(str(constants.Endpoint.PING))(ping)
  301. def _add_optional_endpoints(self):
  302. """Add optional api endpoints (_upload)."""
  303. # To upload files.
  304. if Upload.is_used:
  305. self.api.post(str(constants.Endpoint.UPLOAD))(upload(self))
  306. # To access uploaded files.
  307. self.api.mount(
  308. str(constants.Endpoint.UPLOAD),
  309. StaticFiles(directory=get_upload_dir()),
  310. name="uploaded_files",
  311. )
  312. if codespaces.is_running_in_codespaces():
  313. self.api.get(str(constants.Endpoint.AUTH_CODESPACE))(
  314. codespaces.auth_codespace
  315. )
  316. def _add_cors(self):
  317. """Add CORS middleware to the app."""
  318. self.api.add_middleware(
  319. cors.CORSMiddleware,
  320. allow_credentials=True,
  321. allow_methods=["*"],
  322. allow_headers=["*"],
  323. allow_origins=["*"],
  324. )
  325. @property
  326. def state_manager(self) -> StateManager:
  327. """Get the state manager.
  328. Returns:
  329. The initialized state manager.
  330. Raises:
  331. ValueError: if the state has not been initialized.
  332. """
  333. if self._state_manager is None:
  334. raise ValueError("The state manager has not been initialized.")
  335. return self._state_manager
  336. @staticmethod
  337. def _generate_component(component: Component | ComponentCallable) -> Component:
  338. """Generate a component from a callable.
  339. Args:
  340. component: The component function to call or Component to return as-is.
  341. Returns:
  342. The generated component.
  343. Raises:
  344. VarOperationTypeError: When an invalid component var related function is passed.
  345. TypeError: When an invalid component function is passed.
  346. exceptions.MatchTypeError: If the return types of match cases in rx.match are different.
  347. """
  348. from reflex.utils.exceptions import VarOperationTypeError
  349. try:
  350. return component if isinstance(component, Component) else component()
  351. except exceptions.MatchTypeError:
  352. raise
  353. except TypeError as e:
  354. message = str(e)
  355. if "BaseVar" in message or "ComputedVar" in message:
  356. raise VarOperationTypeError(
  357. "You may be trying to use an invalid Python function on a state var. "
  358. "When referencing a var inside your render code, only limited var operations are supported. "
  359. "See the var operation docs here: https://reflex.dev/docs/vars/var-operations/"
  360. ) from e
  361. raise e
  362. def add_page(
  363. self,
  364. component: Component | ComponentCallable,
  365. route: str | None = None,
  366. title: str | None = None,
  367. description: str | None = None,
  368. image: str = constants.DefaultPage.IMAGE,
  369. on_load: (
  370. EventHandler | EventSpec | list[EventHandler | EventSpec] | None
  371. ) = None,
  372. meta: list[dict[str, str]] = constants.DefaultPage.META_LIST,
  373. ):
  374. """Add a page to the app.
  375. If the component is a callable, by default the route is the name of the
  376. function. Otherwise, a route must be provided.
  377. Args:
  378. component: The component to display at the page.
  379. route: The route to display the component at.
  380. title: The title of the page.
  381. description: The description of the page.
  382. image: The image to display on the page.
  383. on_load: The event handler(s) that will be called each time the page load.
  384. meta: The metadata of the page.
  385. Raises:
  386. ValueError: When the specified route name already exists.
  387. """
  388. # If the route is not set, get it from the callable.
  389. if route is None:
  390. assert isinstance(
  391. component, Callable
  392. ), "Route must be set if component is not a callable."
  393. # Format the route.
  394. route = format.format_route(component.__name__)
  395. else:
  396. route = format.format_route(route, format_case=False)
  397. # Check if the route given is valid
  398. verify_route_validity(route)
  399. if route in self.pages and os.getenv(constants.RELOAD_CONFIG):
  400. # when the app is reloaded(typically for app harness tests), we should maintain
  401. # the latest render function of a route.This applies typically to decorated pages
  402. # since they are only added when app._compile is called.
  403. self.pages.pop(route)
  404. if route in self.pages:
  405. route_name = (
  406. f"`{route}` or `/`"
  407. if route == constants.PageNames.INDEX_ROUTE
  408. else f"`{route}`"
  409. )
  410. raise ValueError(
  411. f"Duplicate page route {route_name} already exists. Make sure you do not have two"
  412. f" pages with the same route"
  413. )
  414. # Setup dynamic args for the route.
  415. # this state assignment is only required for tests using the deprecated state kwarg for App
  416. state = self.state if self.state else State
  417. state.setup_dynamic_args(get_route_args(route))
  418. # Generate the component if it is a callable.
  419. component = self._generate_component(component)
  420. # unpack components that return tuples in an rx.fragment.
  421. if isinstance(component, tuple):
  422. component = Fragment.create(*component)
  423. # Ensure state is enabled if this page uses state.
  424. if self.state is None:
  425. if on_load or component._has_stateful_event_triggers():
  426. self._enable_state()
  427. else:
  428. for var in component._get_vars(include_children=True):
  429. if not var._var_data:
  430. continue
  431. if not var._var_data.state:
  432. continue
  433. self._enable_state()
  434. break
  435. component = OverlayFragment.create(component)
  436. meta_args = {
  437. "title": (
  438. title
  439. if title is not None
  440. else format.make_default_page_title(get_config().app_name, route)
  441. ),
  442. "image": image,
  443. "meta": meta,
  444. }
  445. if description is not None:
  446. meta_args["description"] = description
  447. # Add meta information to the component.
  448. compiler_utils.add_meta(
  449. component,
  450. **meta_args,
  451. )
  452. # Add the page.
  453. self._check_routes_conflict(route)
  454. self.pages[route] = component
  455. # Add the load events.
  456. if on_load:
  457. if not isinstance(on_load, list):
  458. on_load = [on_load]
  459. self.load_events[route] = on_load
  460. def get_load_events(self, route: str) -> list[EventHandler | EventSpec]:
  461. """Get the load events for a route.
  462. Args:
  463. route: The route to get the load events for.
  464. Returns:
  465. The load events for the route.
  466. """
  467. route = route.lstrip("/")
  468. if route == "":
  469. route = constants.PageNames.INDEX_ROUTE
  470. return self.load_events.get(route, [])
  471. def _check_routes_conflict(self, new_route: str):
  472. """Verify if there is any conflict between the new route and any existing route.
  473. Based on conflicts that NextJS would throw if not intercepted.
  474. Raises:
  475. RouteValueError: exception showing which conflict exist with the route to be added
  476. Args:
  477. new_route: the route being newly added.
  478. """
  479. from reflex.utils.exceptions import RouteValueError
  480. if "[" not in new_route:
  481. return
  482. segments = (
  483. constants.RouteRegex.SINGLE_SEGMENT,
  484. constants.RouteRegex.DOUBLE_SEGMENT,
  485. constants.RouteRegex.SINGLE_CATCHALL_SEGMENT,
  486. constants.RouteRegex.DOUBLE_CATCHALL_SEGMENT,
  487. )
  488. for route in self.pages:
  489. replaced_route = replace_brackets_with_keywords(route)
  490. for rw, r, nr in zip(
  491. replaced_route.split("/"), route.split("/"), new_route.split("/")
  492. ):
  493. if rw in segments and r != nr:
  494. # If the slugs in the segments of both routes are not the same, then the route is invalid
  495. raise RouteValueError(
  496. f"You cannot use different slug names for the same dynamic path in {route} and {new_route} ('{r}' != '{nr}')"
  497. )
  498. elif rw not in segments and r != nr:
  499. # if the section being compared in both routes is not a dynamic segment(i.e not wrapped in brackets)
  500. # then we are guaranteed that the route is valid and there's no need checking the rest.
  501. # eg. /posts/[id]/info/[slug1] and /posts/[id]/info1/[slug1] is always going to be valid since
  502. # info1 will break away into its own tree.
  503. break
  504. def add_custom_404_page(
  505. self,
  506. component: Component | ComponentCallable | None = None,
  507. title: str = constants.Page404.TITLE,
  508. image: str = constants.Page404.IMAGE,
  509. description: str = constants.Page404.DESCRIPTION,
  510. on_load: (
  511. EventHandler | EventSpec | list[EventHandler | EventSpec] | None
  512. ) = None,
  513. meta: list[dict[str, str]] = constants.DefaultPage.META_LIST,
  514. ):
  515. """Define a custom 404 page for any url having no match.
  516. If there is no page defined on 'index' route, add the 404 page to it.
  517. If there is no global catchall defined, add the 404 page with a catchall.
  518. Args:
  519. component: The component to display at the page.
  520. title: The title of the page.
  521. description: The description of the page.
  522. image: The image to display on the page.
  523. on_load: The event handler(s) that will be called each time the page load.
  524. meta: The metadata of the page.
  525. """
  526. if component is None:
  527. component = Default404Page.create()
  528. self.add_page(
  529. component=wait_for_client_redirect(self._generate_component(component)),
  530. route=constants.Page404.SLUG,
  531. title=title or constants.Page404.TITLE,
  532. image=image or constants.Page404.IMAGE,
  533. description=description or constants.Page404.DESCRIPTION,
  534. on_load=on_load,
  535. meta=meta,
  536. )
  537. def _setup_admin_dash(self):
  538. """Setup the admin dash."""
  539. # Get the admin dash.
  540. admin_dash = self.admin_dash
  541. if admin_dash and admin_dash.models:
  542. # Build the admin dashboard
  543. admin = (
  544. admin_dash.admin
  545. if admin_dash.admin
  546. else Admin(
  547. engine=Model.get_db_engine(),
  548. title="Reflex Admin Dashboard",
  549. logo_url="https://reflex.dev/Reflex.svg",
  550. )
  551. )
  552. for model in admin_dash.models:
  553. view = admin_dash.view_overrides.get(model, ModelView)
  554. admin.add_view(view(model))
  555. admin.mount_to(self.api)
  556. def _get_frontend_packages(self, imports: Dict[str, set[ImportVar]]):
  557. """Gets the frontend packages to be installed and filters out the unnecessary ones.
  558. Args:
  559. imports: A dictionary containing the imports used in the current page.
  560. Example:
  561. >>> _get_frontend_packages({"react": "16.14.0", "react-dom": "16.14.0"})
  562. """
  563. page_imports = {
  564. i
  565. for i, tags in imports.items()
  566. if i not in constants.PackageJson.DEPENDENCIES
  567. and i not in constants.PackageJson.DEV_DEPENDENCIES
  568. and not any(i.startswith(prefix) for prefix in ["/", ".", "next/"])
  569. and i != ""
  570. and any(tag.install for tag in tags)
  571. }
  572. frontend_packages = get_config().frontend_packages
  573. _frontend_packages = []
  574. for package in frontend_packages:
  575. if package in (get_config().tailwind or {}).get("plugins", []): # type: ignore
  576. console.warn(
  577. f"Tailwind packages are inferred from 'plugins', remove `{package}` from `frontend_packages`"
  578. )
  579. continue
  580. if package in page_imports:
  581. console.warn(
  582. f"React packages and their dependencies are inferred from Component.library and Component.lib_dependencies, remove `{package}` from `frontend_packages`"
  583. )
  584. continue
  585. _frontend_packages.append(package)
  586. page_imports.update(_frontend_packages)
  587. prerequisites.install_frontend_packages(page_imports, get_config())
  588. def _app_root(self, app_wrappers: dict[tuple[int, str], Component]) -> Component:
  589. for component in tuple(app_wrappers.values()):
  590. app_wrappers.update(component._get_all_app_wrap_components())
  591. order = sorted(app_wrappers, key=lambda k: k[0], reverse=True)
  592. root = parent = copy.deepcopy(app_wrappers[order[0]])
  593. for key in order[1:]:
  594. child = copy.deepcopy(app_wrappers[key])
  595. parent.children.append(child)
  596. parent = child
  597. return root
  598. def _should_compile(self) -> bool:
  599. """Check if the app should be compiled.
  600. Returns:
  601. Whether the app should be compiled.
  602. """
  603. # Check the environment variable.
  604. if should_skip_compile():
  605. return False
  606. nocompile = prerequisites.get_web_dir() / constants.NOCOMPILE_FILE
  607. # Check the nocompile file.
  608. if nocompile.exists():
  609. # Delete the nocompile file
  610. nocompile.unlink()
  611. return False
  612. # By default, compile the app.
  613. return True
  614. def _add_overlay_to_component(self, component: Component) -> Component:
  615. if self.overlay_component is None:
  616. return component
  617. children = component.children
  618. overlay_component = self._generate_component(self.overlay_component)
  619. if children[0] == overlay_component:
  620. return component
  621. # recreate OverlayFragment with overlay_component as first child
  622. component = OverlayFragment.create(overlay_component, *children)
  623. return component
  624. def _setup_overlay_component(self):
  625. """If a State is not used and no overlay_component is specified, do not render the connection modal."""
  626. if self.state is None and self.overlay_component is default_overlay_component:
  627. self.overlay_component = None
  628. for k, component in self.pages.items():
  629. self.pages[k] = self._add_overlay_to_component(component)
  630. def _add_error_boundary_to_component(self, component: Component) -> Component:
  631. if self.error_boundary is None:
  632. return component
  633. component = self.error_boundary(*component.children)
  634. return component
  635. def _setup_error_boundary(self):
  636. """If a State is not used and no error_boundary is specified, do not render the error boundary."""
  637. if self.state is None and self.error_boundary is default_error_boundary:
  638. self.error_boundary = None
  639. for k, component in self.pages.items():
  640. # Skip the 404 page
  641. if k == constants.Page404.SLUG:
  642. continue
  643. self.pages[k] = self._add_error_boundary_to_component(component)
  644. def _apply_decorated_pages(self):
  645. """Add @rx.page decorated pages to the app.
  646. This has to be done in the MainThread for py38 and py39 compatibility, so the
  647. decorated pages are added to the app before the app is compiled (in a thread)
  648. to workaround REF-2172.
  649. This can move back into `compile_` when py39 support is dropped.
  650. """
  651. # Add the @rx.page decorated pages to collect on_load events.
  652. for render, kwargs in DECORATED_PAGES[get_config().app_name]:
  653. self.add_page(render, **kwargs)
  654. def _validate_var_dependencies(
  655. self, state: Optional[Type[BaseState]] = None
  656. ) -> None:
  657. """Validate the dependencies of the vars in the app.
  658. Args:
  659. state: The state to validate the dependencies for.
  660. Raises:
  661. VarDependencyError: When a computed var has an invalid dependency.
  662. """
  663. if not self.state:
  664. return
  665. if not state:
  666. state = self.state
  667. for var in state.computed_vars.values():
  668. if not var._cache:
  669. continue
  670. deps = var._deps(objclass=state)
  671. for dep in deps:
  672. if dep not in state.vars and dep not in state.backend_vars:
  673. raise exceptions.VarDependencyError(
  674. f"ComputedVar {var._var_name} on state {state.__name__} has an invalid dependency {dep}"
  675. )
  676. for substate in state.class_subclasses:
  677. self._validate_var_dependencies(substate)
  678. def _compile(self, export: bool = False):
  679. """Compile the app and output it to the pages folder.
  680. Args:
  681. export: Whether to compile the app for export.
  682. Raises:
  683. ReflexRuntimeError: When any page uses state, but no rx.State subclass is defined.
  684. """
  685. from reflex.utils.exceptions import ReflexRuntimeError
  686. def get_compilation_time() -> str:
  687. return str(datetime.now().time()).split(".")[0]
  688. # Render a default 404 page if the user didn't supply one
  689. if constants.Page404.SLUG not in self.pages:
  690. self.add_custom_404_page()
  691. # Add the optional endpoints (_upload)
  692. self._add_optional_endpoints()
  693. if not self._should_compile():
  694. return
  695. self._validate_var_dependencies()
  696. self._setup_overlay_component()
  697. self._setup_error_boundary()
  698. # Create a progress bar.
  699. progress = Progress(
  700. *Progress.get_default_columns()[:-1],
  701. MofNCompleteColumn(),
  702. TimeElapsedColumn(),
  703. )
  704. # try to be somewhat accurate - but still not 100%
  705. adhoc_steps_without_executor = 6
  706. fixed_pages_within_executor = 5
  707. progress.start()
  708. task = progress.add_task(
  709. f"[{get_compilation_time()}] Compiling:",
  710. total=len(self.pages)
  711. + fixed_pages_within_executor
  712. + adhoc_steps_without_executor,
  713. )
  714. # Get the env mode.
  715. config = get_config()
  716. # Store the compile results.
  717. compile_results = []
  718. # Add the app wrappers.
  719. app_wrappers: Dict[tuple[int, str], Component] = {
  720. # Default app wrap component renders {children}
  721. (0, "AppWrap"): AppWrap.create()
  722. }
  723. if self.theme is not None:
  724. # If a theme component was provided, wrap the app with it
  725. app_wrappers[(20, "Theme")] = self.theme
  726. progress.advance(task)
  727. # Fix up the style.
  728. self.style = evaluate_style_namespaces(self.style)
  729. # Track imports and custom components found.
  730. all_imports = {}
  731. custom_components = set()
  732. for _route, component in self.pages.items():
  733. # Merge the component style with the app style.
  734. component._add_style_recursive(self.style, self.theme)
  735. # Add component._get_all_imports() to all_imports.
  736. all_imports.update(component._get_all_imports())
  737. # Add the app wrappers from this component.
  738. app_wrappers.update(component._get_all_app_wrap_components())
  739. # Add the custom components from the page to the set.
  740. custom_components |= component._get_all_custom_components()
  741. progress.advance(task)
  742. # Perform auto-memoization of stateful components.
  743. (
  744. stateful_components_path,
  745. stateful_components_code,
  746. page_components,
  747. ) = compiler.compile_stateful_components(self.pages.values())
  748. progress.advance(task)
  749. # Catch "static" apps (that do not define a rx.State subclass) which are trying to access rx.State.
  750. if code_uses_state_contexts(stateful_components_code) and self.state is None:
  751. raise ReflexRuntimeError(
  752. "To access rx.State in frontend components, at least one "
  753. "subclass of rx.State must be defined in the app."
  754. )
  755. compile_results.append((stateful_components_path, stateful_components_code))
  756. # Compile the root document before fork.
  757. compile_results.append(
  758. compiler.compile_document_root(
  759. self.head_components,
  760. html_lang=self.html_lang,
  761. html_custom_attrs=self.html_custom_attrs, # type: ignore
  762. )
  763. )
  764. # Compile the contexts before fork.
  765. compile_results.append(
  766. compiler.compile_contexts(self.state, self.theme),
  767. )
  768. # Fix #2992 by removing the top-level appearance prop
  769. if self.theme is not None:
  770. self.theme.appearance = None
  771. app_root = self._app_root(app_wrappers=app_wrappers)
  772. progress.advance(task)
  773. # Prepopulate the global ExecutorSafeFunctions class with input data required by the compile functions.
  774. # This is required for multiprocessing to work, in presence of non-picklable inputs.
  775. for route, component in zip(self.pages, page_components):
  776. ExecutorSafeFunctions.COMPILE_PAGE_ARGS_BY_ROUTE[route] = (
  777. route,
  778. component,
  779. self.state,
  780. )
  781. ExecutorSafeFunctions.COMPILE_APP_APP_ROOT = app_root
  782. ExecutorSafeFunctions.CUSTOM_COMPONENTS = custom_components
  783. ExecutorSafeFunctions.STYLE = self.style
  784. # Use a forking process pool, if possible. Much faster, especially for large sites.
  785. # Fallback to ThreadPoolExecutor as something that will always work.
  786. executor = None
  787. if (
  788. platform.system() in ("Linux", "Darwin")
  789. and os.environ.get("REFLEX_COMPILE_PROCESSES") is not None
  790. ):
  791. executor = concurrent.futures.ProcessPoolExecutor(
  792. max_workers=int(os.environ.get("REFLEX_COMPILE_PROCESSES", 0)) or None,
  793. mp_context=multiprocessing.get_context("fork"),
  794. )
  795. else:
  796. executor = concurrent.futures.ThreadPoolExecutor(
  797. max_workers=int(os.environ.get("REFLEX_COMPILE_THREADS", 0)) or None,
  798. )
  799. with executor:
  800. result_futures = []
  801. custom_components_future = None
  802. def _mark_complete(_=None):
  803. progress.advance(task)
  804. def _submit_work(fn, *args, **kwargs):
  805. f = executor.submit(fn, *args, **kwargs)
  806. f.add_done_callback(_mark_complete)
  807. result_futures.append(f)
  808. # Compile all page components.
  809. for route in self.pages:
  810. _submit_work(ExecutorSafeFunctions.compile_page, route)
  811. # Compile the app wrapper.
  812. _submit_work(ExecutorSafeFunctions.compile_app)
  813. # Compile the custom components.
  814. custom_components_future = executor.submit(
  815. ExecutorSafeFunctions.compile_custom_components,
  816. )
  817. custom_components_future.add_done_callback(_mark_complete)
  818. # Compile the root stylesheet with base styles.
  819. _submit_work(compiler.compile_root_stylesheet, self.stylesheets)
  820. # Compile the theme.
  821. _submit_work(ExecutorSafeFunctions.compile_theme)
  822. # Compile the Tailwind config.
  823. if config.tailwind is not None:
  824. config.tailwind["content"] = config.tailwind.get(
  825. "content", constants.Tailwind.CONTENT
  826. )
  827. _submit_work(compiler.compile_tailwind, config.tailwind)
  828. else:
  829. _submit_work(compiler.remove_tailwind_from_postcss)
  830. # Wait for all compilation tasks to complete.
  831. for future in concurrent.futures.as_completed(result_futures):
  832. compile_results.append(future.result())
  833. # Special case for custom_components, since we need the compiled imports
  834. # to install proper frontend packages.
  835. (
  836. *custom_components_result,
  837. custom_components_imports,
  838. ) = custom_components_future.result()
  839. compile_results.append(custom_components_result)
  840. all_imports.update(custom_components_imports)
  841. # Get imports from AppWrap components.
  842. all_imports.update(app_root._get_all_imports())
  843. progress.advance(task)
  844. progress.advance(task)
  845. progress.stop()
  846. # Install frontend packages.
  847. self._get_frontend_packages(all_imports)
  848. # Setup the next.config.js
  849. transpile_packages = [
  850. package
  851. for package, import_vars in all_imports.items()
  852. if any(import_var.transpile for import_var in import_vars)
  853. ]
  854. prerequisites.update_next_config(
  855. export=export,
  856. transpile_packages=transpile_packages,
  857. )
  858. if is_prod_mode():
  859. # Empty the .web pages directory.
  860. compiler.purge_web_pages_dir()
  861. else:
  862. # In dev mode, delete removed pages and update existing pages.
  863. keep_files = [Path(output_path) for output_path, _ in compile_results]
  864. for p in Path(prerequisites.get_web_dir() / constants.Dirs.PAGES).rglob(
  865. "*"
  866. ):
  867. if p.is_file() and p not in keep_files:
  868. # Remove pages that are no longer in the app.
  869. p.unlink()
  870. for output_path, code in compile_results:
  871. compiler_utils.write_page(output_path, code)
  872. @contextlib.asynccontextmanager
  873. async def modify_state(self, token: str) -> AsyncIterator[BaseState]:
  874. """Modify the state out of band.
  875. Args:
  876. token: The token to modify the state for.
  877. Yields:
  878. The state to modify.
  879. Raises:
  880. RuntimeError: If the app has not been initialized yet.
  881. """
  882. if self.event_namespace is None:
  883. raise RuntimeError("App has not been initialized yet.")
  884. # Get exclusive access to the state.
  885. async with self.state_manager.modify_state(token) as state:
  886. # No other event handler can modify the state while in this context.
  887. yield state
  888. delta = state.get_delta()
  889. if delta:
  890. # When the state is modified reset dirty status and emit the delta to the frontend.
  891. state._clean()
  892. await self.event_namespace.emit_update(
  893. update=StateUpdate(delta=delta),
  894. sid=state.router.session.session_id,
  895. )
  896. def _process_background(
  897. self, state: BaseState, event: Event
  898. ) -> asyncio.Task | None:
  899. """Process an event in the background and emit updates as they arrive.
  900. Args:
  901. state: The state to process the event for.
  902. event: The event to process.
  903. Returns:
  904. Task if the event was backgroundable, otherwise None
  905. """
  906. substate, handler = state._get_event_handler(event)
  907. if not handler.is_background:
  908. return None
  909. async def _coro():
  910. """Coroutine to process the event and emit updates inside an asyncio.Task.
  911. Raises:
  912. RuntimeError: If the app has not been initialized yet.
  913. """
  914. if self.event_namespace is None:
  915. raise RuntimeError("App has not been initialized yet.")
  916. # Process the event.
  917. async for update in state._process_event(
  918. handler=handler, state=substate, payload=event.payload
  919. ):
  920. # Postprocess the event.
  921. update = await self._postprocess(state, event, update)
  922. # Send the update to the client.
  923. await self.event_namespace.emit_update(
  924. update=update,
  925. sid=state.router.session.session_id,
  926. )
  927. task = asyncio.create_task(_coro())
  928. self.background_tasks.add(task)
  929. # Clean up task from background_tasks set when complete.
  930. task.add_done_callback(self.background_tasks.discard)
  931. return task
  932. def _validate_exception_handlers(self):
  933. """Validate the custom event exception handlers for front- and backend.
  934. Raises:
  935. ValueError: If the custom exception handlers are invalid.
  936. """
  937. FRONTEND_ARG_SPEC = {
  938. "exception": Exception,
  939. }
  940. BACKEND_ARG_SPEC = {
  941. "exception": Exception,
  942. }
  943. for handler_domain, handler_fn, handler_spec in zip(
  944. ["frontend", "backend"],
  945. [self.frontend_exception_handler, self.backend_exception_handler],
  946. [
  947. FRONTEND_ARG_SPEC,
  948. BACKEND_ARG_SPEC,
  949. ],
  950. ):
  951. if hasattr(handler_fn, "__name__"):
  952. _fn_name = handler_fn.__name__
  953. else:
  954. _fn_name = handler_fn.__class__.__name__
  955. if isinstance(handler_fn, functools.partial):
  956. raise ValueError(
  957. f"Provided custom {handler_domain} exception handler `{_fn_name}` is a partial function. Please provide a named function instead."
  958. )
  959. if not callable(handler_fn):
  960. raise ValueError(
  961. f"Provided custom {handler_domain} exception handler `{_fn_name}` is not a function."
  962. )
  963. # Allow named functions only as lambda functions cannot be introspected
  964. if _fn_name == "<lambda>":
  965. raise ValueError(
  966. f"Provided custom {handler_domain} exception handler `{_fn_name}` is a lambda function. Please use a named function instead."
  967. )
  968. # Check if the function has the necessary annotations and types in the right order
  969. argspec = inspect.getfullargspec(handler_fn)
  970. arg_annotations = {
  971. k: eval(v) if isinstance(v, str) else v
  972. for k, v in argspec.annotations.items()
  973. if k not in ["args", "kwargs", "return"]
  974. }
  975. for required_arg_index, required_arg in enumerate(handler_spec):
  976. if required_arg not in arg_annotations:
  977. raise ValueError(
  978. f"Provided custom {handler_domain} exception handler `{_fn_name}` does not take the required argument `{required_arg}`"
  979. )
  980. elif (
  981. not list(arg_annotations.keys())[required_arg_index] == required_arg
  982. ):
  983. raise ValueError(
  984. f"Provided custom {handler_domain} exception handler `{_fn_name}` has the wrong argument order."
  985. f"Expected `{required_arg}` as the {required_arg_index+1} argument but got `{list(arg_annotations.keys())[required_arg_index]}`"
  986. )
  987. if not issubclass(arg_annotations[required_arg], Exception):
  988. raise ValueError(
  989. f"Provided custom {handler_domain} exception handler `{_fn_name}` has the wrong type for {required_arg} argument."
  990. f"Expected to be `Exception` but got `{arg_annotations[required_arg]}`"
  991. )
  992. # Check if the return type is valid for backend exception handler
  993. if handler_domain == "backend":
  994. sig = inspect.signature(self.backend_exception_handler)
  995. return_type = (
  996. eval(sig.return_annotation)
  997. if isinstance(sig.return_annotation, str)
  998. else sig.return_annotation
  999. )
  1000. valid = bool(
  1001. return_type == EventSpec
  1002. or return_type == Optional[EventSpec]
  1003. or return_type == List[EventSpec]
  1004. or return_type == inspect.Signature.empty
  1005. or return_type is None
  1006. )
  1007. if not valid:
  1008. raise ValueError(
  1009. f"Provided custom {handler_domain} exception handler `{_fn_name}` has the wrong return type."
  1010. f"Expected `Union[EventSpec, List[EventSpec], None]` but got `{return_type}`"
  1011. )
  1012. async def process(
  1013. app: App, event: Event, sid: str, headers: Dict, client_ip: str
  1014. ) -> AsyncIterator[StateUpdate]:
  1015. """Process an event.
  1016. Args:
  1017. app: The app to process the event for.
  1018. event: The event to process.
  1019. sid: The Socket.IO session id.
  1020. headers: The client headers.
  1021. client_ip: The client_ip.
  1022. Raises:
  1023. Exception: If a reflex specific error occurs during processing the event.
  1024. Yields:
  1025. The state updates after processing the event.
  1026. """
  1027. from reflex.utils import telemetry
  1028. try:
  1029. # Add request data to the state.
  1030. router_data = event.router_data
  1031. router_data.update(
  1032. {
  1033. constants.RouteVar.QUERY: format.format_query_params(event.router_data),
  1034. constants.RouteVar.CLIENT_TOKEN: event.token,
  1035. constants.RouteVar.SESSION_ID: sid,
  1036. constants.RouteVar.HEADERS: headers,
  1037. constants.RouteVar.CLIENT_IP: client_ip,
  1038. }
  1039. )
  1040. # Get the state for the session exclusively.
  1041. async with app.state_manager.modify_state(event.substate_token) as state:
  1042. # re-assign only when the value is different
  1043. if state.router_data != router_data:
  1044. # assignment will recurse into substates and force recalculation of
  1045. # dependent ComputedVar (dynamic route variables)
  1046. state.router_data = router_data
  1047. state.router = RouterData(router_data)
  1048. # Preprocess the event.
  1049. update = await app._preprocess(state, event)
  1050. # If there was an update, yield it.
  1051. if update is not None:
  1052. yield update
  1053. # Only process the event if there is no update.
  1054. else:
  1055. if app._process_background(state, event) is not None:
  1056. # `final=True` allows the frontend send more events immediately.
  1057. yield StateUpdate(final=True)
  1058. return
  1059. # Process the event synchronously.
  1060. async for update in state._process(event):
  1061. # Postprocess the event.
  1062. update = await app._postprocess(state, event, update)
  1063. # Yield the update.
  1064. yield update
  1065. except Exception as ex:
  1066. telemetry.send_error(ex, context="backend")
  1067. app.backend_exception_handler(ex)
  1068. raise
  1069. async def ping() -> str:
  1070. """Test API endpoint.
  1071. Returns:
  1072. The response.
  1073. """
  1074. return "pong"
  1075. def upload(app: App):
  1076. """Upload a file.
  1077. Args:
  1078. app: The app to upload the file for.
  1079. Returns:
  1080. The upload function.
  1081. """
  1082. async def upload_file(request: Request, files: List[UploadFile]):
  1083. """Upload a file.
  1084. Args:
  1085. request: The FastAPI request object.
  1086. files: The file(s) to upload.
  1087. Returns:
  1088. StreamingResponse yielding newline-delimited JSON of StateUpdate
  1089. emitted by the upload handler.
  1090. Raises:
  1091. UploadValueError: if there are no args with supported annotation.
  1092. UploadTypeError: if a background task is used as the handler.
  1093. HTTPException: when the request does not include token / handler headers.
  1094. """
  1095. from reflex.utils.exceptions import UploadTypeError, UploadValueError
  1096. token = request.headers.get("reflex-client-token")
  1097. handler = request.headers.get("reflex-event-handler")
  1098. if not token or not handler:
  1099. raise HTTPException(
  1100. status_code=400,
  1101. detail="Missing reflex-client-token or reflex-event-handler header.",
  1102. )
  1103. # Get the state for the session.
  1104. substate_token = _substate_key(token, handler.rpartition(".")[0])
  1105. state = await app.state_manager.get_state(substate_token)
  1106. # get the current session ID
  1107. # get the current state(parent state/substate)
  1108. path = handler.split(".")[:-1]
  1109. current_state = state.get_substate(path)
  1110. handler_upload_param = ()
  1111. # get handler function
  1112. func = getattr(type(current_state), handler.split(".")[-1])
  1113. # check if there exists any handler args with annotation, List[UploadFile]
  1114. if isinstance(func, EventHandler):
  1115. if func.is_background:
  1116. raise UploadTypeError(
  1117. f"@rx.background is not supported for upload handler `{handler}`.",
  1118. )
  1119. func = func.fn
  1120. if isinstance(func, functools.partial):
  1121. func = func.func
  1122. for k, v in get_type_hints(func).items():
  1123. if types.is_generic_alias(v) and types._issubclass(
  1124. get_args(v)[0],
  1125. UploadFile,
  1126. ):
  1127. handler_upload_param = (k, v)
  1128. break
  1129. if not handler_upload_param:
  1130. raise UploadValueError(
  1131. f"`{handler}` handler should have a parameter annotated as "
  1132. "List[rx.UploadFile]"
  1133. )
  1134. # Make a copy of the files as they are closed after the request.
  1135. # This behaviour changed from fastapi 0.103.0 to 0.103.1 as the
  1136. # AsyncExitStack was removed from the request scope and is now
  1137. # part of the routing function which closes this before the
  1138. # event is handled.
  1139. file_copies = []
  1140. for file in files:
  1141. content_copy = io.BytesIO()
  1142. content_copy.write(await file.read())
  1143. content_copy.seek(0)
  1144. file_copies.append(
  1145. UploadFile(
  1146. file=content_copy,
  1147. filename=file.filename,
  1148. size=file.size,
  1149. headers=file.headers,
  1150. )
  1151. )
  1152. event = Event(
  1153. token=token,
  1154. name=handler,
  1155. payload={handler_upload_param[0]: file_copies},
  1156. )
  1157. async def _ndjson_updates():
  1158. """Process the upload event, generating ndjson updates.
  1159. Yields:
  1160. Each state update as JSON followed by a new line.
  1161. """
  1162. # Process the event.
  1163. async with app.state_manager.modify_state(event.substate_token) as state:
  1164. async for update in state._process(event):
  1165. # Postprocess the event.
  1166. update = await app._postprocess(state, event, update)
  1167. yield update.json() + "\n"
  1168. # Stream updates to client
  1169. return StreamingResponse(
  1170. _ndjson_updates(),
  1171. media_type="application/x-ndjson",
  1172. )
  1173. return upload_file
  1174. class EventNamespace(AsyncNamespace):
  1175. """The event namespace."""
  1176. # The application object.
  1177. app: App
  1178. # Keep a mapping between socket ID and client token.
  1179. token_to_sid: dict[str, str] = {}
  1180. # Keep a mapping between client token and socket ID.
  1181. sid_to_token: dict[str, str] = {}
  1182. def __init__(self, namespace: str, app: App):
  1183. """Initialize the event namespace.
  1184. Args:
  1185. namespace: The namespace.
  1186. app: The application object.
  1187. """
  1188. super().__init__(namespace)
  1189. self.app = app
  1190. def on_connect(self, sid, environ):
  1191. """Event for when the websocket is connected.
  1192. Args:
  1193. sid: The Socket.IO session id.
  1194. environ: The request information, including HTTP headers.
  1195. """
  1196. pass
  1197. def on_disconnect(self, sid):
  1198. """Event for when the websocket disconnects.
  1199. Args:
  1200. sid: The Socket.IO session id.
  1201. """
  1202. disconnect_token = self.sid_to_token.pop(sid, None)
  1203. if disconnect_token:
  1204. self.token_to_sid.pop(disconnect_token, None)
  1205. async def emit_update(self, update: StateUpdate, sid: str) -> None:
  1206. """Emit an update to the client.
  1207. Args:
  1208. update: The state update to send.
  1209. sid: The Socket.IO session id.
  1210. """
  1211. # Creating a task prevents the update from being blocked behind other coroutines.
  1212. await asyncio.create_task(
  1213. self.emit(str(constants.SocketEvent.EVENT), update.json(), to=sid)
  1214. )
  1215. async def on_event(self, sid, data):
  1216. """Event for receiving front-end websocket events.
  1217. Args:
  1218. sid: The Socket.IO session id.
  1219. data: The event data.
  1220. """
  1221. # Get the event.
  1222. event = Event.parse_raw(data)
  1223. self.token_to_sid[event.token] = sid
  1224. self.sid_to_token[sid] = event.token
  1225. # Get the event environment.
  1226. assert self.app.sio is not None
  1227. environ = self.app.sio.get_environ(sid, self.namespace)
  1228. assert environ is not None
  1229. # Get the client headers.
  1230. headers = {
  1231. k.decode("utf-8"): v.decode("utf-8")
  1232. for (k, v) in environ["asgi.scope"]["headers"]
  1233. }
  1234. # Get the client IP
  1235. try:
  1236. client_ip = environ["asgi.scope"]["client"][0]
  1237. except (KeyError, IndexError):
  1238. client_ip = environ.get("REMOTE_ADDR", "0.0.0.0")
  1239. # Process the events.
  1240. async for update in process(self.app, event, sid, headers, client_ip):
  1241. # Emit the update from processing the event.
  1242. await self.emit_update(update=update, sid=sid)
  1243. async def on_ping(self, sid):
  1244. """Event for testing the API endpoint.
  1245. Args:
  1246. sid: The Socket.IO session id.
  1247. """
  1248. # Emit the test event.
  1249. await self.emit(str(constants.SocketEvent.PING), "pong", to=sid)