app.py 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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 multiprocessing
  9. import os
  10. import platform
  11. from typing import (
  12. Any,
  13. AsyncIterator,
  14. Callable,
  15. Coroutine,
  16. Dict,
  17. List,
  18. Optional,
  19. Set,
  20. Type,
  21. Union,
  22. get_args,
  23. get_type_hints,
  24. )
  25. from fastapi import FastAPI, HTTPException, Request, UploadFile
  26. from fastapi.middleware import cors
  27. from fastapi.responses import StreamingResponse
  28. from fastapi.staticfiles import StaticFiles
  29. from rich.progress import MofNCompleteColumn, Progress, TimeElapsedColumn
  30. from socketio import ASGIApp, AsyncNamespace, AsyncServer
  31. from starlette_admin.contrib.sqla.admin import Admin
  32. from starlette_admin.contrib.sqla.view import ModelView
  33. from reflex import constants
  34. from reflex.admin import AdminDash
  35. from reflex.base import Base
  36. from reflex.compiler import compiler
  37. from reflex.compiler import utils as compiler_utils
  38. from reflex.compiler.compiler import ExecutorSafeFunctions
  39. from reflex.components import connection_modal, connection_pulser
  40. from reflex.components.base.app_wrap import AppWrap
  41. from reflex.components.base.fragment import Fragment
  42. from reflex.components.component import (
  43. Component,
  44. ComponentStyle,
  45. evaluate_style_namespaces,
  46. )
  47. from reflex.components.core.client_side_routing import (
  48. Default404Page,
  49. wait_for_client_redirect,
  50. )
  51. from reflex.components.core.upload import Upload, get_upload_dir
  52. from reflex.components.radix import themes
  53. from reflex.config import get_config
  54. from reflex.event import Event, EventHandler, EventSpec
  55. from reflex.middleware import HydrateMiddleware, Middleware
  56. from reflex.model import Model
  57. from reflex.page import (
  58. DECORATED_PAGES,
  59. )
  60. from reflex.route import (
  61. catchall_in_route,
  62. catchall_prefix,
  63. get_route_args,
  64. verify_route_validity,
  65. )
  66. from reflex.state import (
  67. BaseState,
  68. RouterData,
  69. State,
  70. StateManager,
  71. StateUpdate,
  72. _substate_key,
  73. code_uses_state_contexts,
  74. )
  75. from reflex.utils import console, exceptions, format, prerequisites, types
  76. from reflex.utils.exec import is_testing_env, should_skip_compile
  77. from reflex.utils.imports import ImportVar
  78. # Define custom types.
  79. ComponentCallable = Callable[[], Component]
  80. Reducer = Callable[[Event], Coroutine[Any, Any, StateUpdate]]
  81. def default_overlay_component() -> Component:
  82. """Default overlay_component attribute for App.
  83. Returns:
  84. The default overlay_component, which is a connection_modal.
  85. """
  86. return Fragment.create(connection_pulser(), connection_modal())
  87. class OverlayFragment(Fragment):
  88. """Alias for Fragment, used to wrap the overlay_component."""
  89. pass
  90. class App(Base):
  91. """A Reflex application."""
  92. # A map from a page route to the component to render.
  93. pages: Dict[str, Component] = {}
  94. # A list of URLs to stylesheets to include in the app.
  95. stylesheets: List[str] = []
  96. # The backend API object.
  97. api: FastAPI = None # type: ignore
  98. # The Socket.IO AsyncServer.
  99. sio: Optional[AsyncServer] = None
  100. # The socket app.
  101. socket_app: Optional[ASGIApp] = None
  102. # The state class to use for the app.
  103. state: Optional[Type[BaseState]] = None
  104. # Class to manage many client states.
  105. _state_manager: Optional[StateManager] = None
  106. # The styling to apply to each component.
  107. style: ComponentStyle = {}
  108. # Middleware to add to the app.
  109. middleware: List[Middleware] = []
  110. # List of event handlers to trigger when a page loads.
  111. load_events: Dict[str, List[Union[EventHandler, EventSpec]]] = {}
  112. # Admin dashboard
  113. admin_dash: Optional[AdminDash] = None
  114. # The async server name space
  115. event_namespace: Optional[EventNamespace] = None
  116. # Components to add to the head of every page.
  117. head_components: List[Component] = []
  118. # The language to add to the html root tag of every page.
  119. html_lang: Optional[str] = None
  120. # Attributes to add to the html root tag of every page.
  121. html_custom_attrs: Optional[Dict[str, str]] = None
  122. # A component that is present on every page.
  123. overlay_component: Optional[
  124. Union[Component, ComponentCallable]
  125. ] = default_overlay_component
  126. # Background tasks that are currently running
  127. background_tasks: Set[asyncio.Task] = set()
  128. # The radix theme for the entire app
  129. theme: Optional[Component] = themes.theme(accent_color="blue")
  130. def __init__(self, *args, **kwargs):
  131. """Initialize the app.
  132. Args:
  133. *args: Args to initialize the app with.
  134. **kwargs: Kwargs to initialize the app with.
  135. Raises:
  136. ValueError: If the event namespace is not provided in the config.
  137. Also, if there are multiple client subclasses of rx.BaseState(Subclasses of rx.BaseState should consist
  138. of the DefaultState and the client app state).
  139. """
  140. if "connect_error_component" in kwargs:
  141. raise ValueError(
  142. "`connect_error_component` is deprecated, use `overlay_component` instead"
  143. )
  144. super().__init__(*args, **kwargs)
  145. base_state_subclasses = BaseState.__subclasses__()
  146. # Special case to allow test cases have multiple subclasses of rx.BaseState.
  147. if not is_testing_env():
  148. # Only one Base State class is allowed.
  149. if len(base_state_subclasses) > 1:
  150. raise ValueError(
  151. "rx.BaseState cannot be subclassed multiple times. use rx.State instead"
  152. )
  153. if "state" in kwargs:
  154. console.deprecate(
  155. feature_name="`state` argument for App()",
  156. reason="due to all `rx.State` subclasses being inferred.",
  157. deprecation_version="0.3.5",
  158. removal_version="0.5.0",
  159. )
  160. # Add middleware.
  161. self.middleware.append(HydrateMiddleware())
  162. # Set up the API.
  163. self.api = FastAPI()
  164. self.add_cors()
  165. self.add_default_endpoints()
  166. self.setup_state()
  167. # Set up the admin dash.
  168. self.setup_admin_dash()
  169. def enable_state(self) -> None:
  170. """Enable state for the app."""
  171. if not self.state:
  172. self.state = State
  173. self.setup_state()
  174. def setup_state(self) -> None:
  175. """Set up the state for the app.
  176. Raises:
  177. ValueError: If the event namespace is not provided in the config.
  178. If the state has not been enabled.
  179. """
  180. if not self.state:
  181. return
  182. config = get_config()
  183. # Set up the state manager.
  184. self._state_manager = StateManager.create(state=self.state)
  185. # Set up the Socket.IO AsyncServer.
  186. self.sio = AsyncServer(
  187. async_mode="asgi",
  188. cors_allowed_origins=(
  189. "*"
  190. if config.cors_allowed_origins == ["*"]
  191. else config.cors_allowed_origins
  192. ),
  193. cors_credentials=True,
  194. max_http_buffer_size=constants.POLLING_MAX_HTTP_BUFFER_SIZE,
  195. ping_interval=constants.Ping.INTERVAL,
  196. ping_timeout=constants.Ping.TIMEOUT,
  197. )
  198. # Create the socket app. Note event endpoint constant replaces the default 'socket.io' path.
  199. self.socket_app = ASGIApp(self.sio, socketio_path="")
  200. namespace = config.get_event_namespace()
  201. if not namespace:
  202. raise ValueError("event namespace must be provided in the config.")
  203. # Create the event namespace and attach the main app. Not related to any paths.
  204. self.event_namespace = EventNamespace(namespace, self)
  205. # Register the event namespace with the socket.
  206. self.sio.register_namespace(self.event_namespace)
  207. # Mount the socket app with the API.
  208. self.api.mount(str(constants.Endpoint.EVENT), self.socket_app)
  209. def __repr__(self) -> str:
  210. """Get the string representation of the app.
  211. Returns:
  212. The string representation of the app.
  213. """
  214. return f"<App state={self.state.__name__ if self.state else None}>"
  215. def __call__(self) -> FastAPI:
  216. """Run the backend api instance.
  217. Returns:
  218. The backend api.
  219. """
  220. return self.api
  221. def add_default_endpoints(self):
  222. """Add default api endpoints (ping)."""
  223. # To test the server.
  224. self.api.get(str(constants.Endpoint.PING))(ping)
  225. def add_optional_endpoints(self):
  226. """Add optional api endpoints (_upload)."""
  227. # To upload files.
  228. if Upload.is_used:
  229. self.api.post(str(constants.Endpoint.UPLOAD))(upload(self))
  230. # To access uploaded files.
  231. self.api.mount(
  232. str(constants.Endpoint.UPLOAD),
  233. StaticFiles(directory=get_upload_dir()),
  234. name="uploaded_files",
  235. )
  236. def add_cors(self):
  237. """Add CORS middleware to the app."""
  238. self.api.add_middleware(
  239. cors.CORSMiddleware,
  240. allow_credentials=True,
  241. allow_methods=["*"],
  242. allow_headers=["*"],
  243. allow_origins=["*"],
  244. )
  245. @property
  246. def state_manager(self) -> StateManager:
  247. """Get the state manager.
  248. Returns:
  249. The initialized state manager.
  250. Raises:
  251. ValueError: if the state has not been initialized.
  252. """
  253. if self._state_manager is None:
  254. raise ValueError("The state manager has not been initialized.")
  255. return self._state_manager
  256. async def preprocess(self, state: BaseState, event: Event) -> StateUpdate | None:
  257. """Preprocess the event.
  258. This is where middleware can modify the event before it is processed.
  259. Each middleware is called in the order it was added to the app.
  260. If a middleware returns an update, the event is not processed and the
  261. update is returned.
  262. Args:
  263. state: The state to preprocess.
  264. event: The event to preprocess.
  265. Returns:
  266. An optional state to return.
  267. """
  268. for middleware in self.middleware:
  269. if asyncio.iscoroutinefunction(middleware.preprocess):
  270. out = await middleware.preprocess(app=self, state=state, event=event) # type: ignore
  271. else:
  272. out = middleware.preprocess(app=self, state=state, event=event) # type: ignore
  273. if out is not None:
  274. return out # type: ignore
  275. async def postprocess(
  276. self, state: BaseState, event: Event, update: StateUpdate
  277. ) -> StateUpdate:
  278. """Postprocess the event.
  279. This is where middleware can modify the delta after it is processed.
  280. Each middleware is called in the order it was added to the app.
  281. Args:
  282. state: The state to postprocess.
  283. event: The event to postprocess.
  284. update: The current state update.
  285. Returns:
  286. The state update to return.
  287. """
  288. for middleware in self.middleware:
  289. if asyncio.iscoroutinefunction(middleware.postprocess):
  290. out = await middleware.postprocess(
  291. app=self, state=state, event=event, update=update # type: ignore
  292. )
  293. else:
  294. out = middleware.postprocess(
  295. app=self, state=state, event=event, update=update # type: ignore
  296. )
  297. if out is not None:
  298. return out # type: ignore
  299. return update
  300. def add_middleware(self, middleware: Middleware, index: int | None = None):
  301. """Add middleware to the app.
  302. Args:
  303. middleware: The middleware to add.
  304. index: The index to add the middleware at.
  305. """
  306. if index is None:
  307. self.middleware.append(middleware)
  308. else:
  309. self.middleware.insert(index, middleware)
  310. @staticmethod
  311. def _generate_component(component: Component | ComponentCallable) -> Component:
  312. """Generate a component from a callable.
  313. Args:
  314. component: The component function to call or Component to return as-is.
  315. Returns:
  316. The generated component.
  317. Raises:
  318. TypeError: When an invalid component function is passed.
  319. exceptions.MatchTypeError: If the return types of match cases in rx.match are different.
  320. """
  321. try:
  322. return component if isinstance(component, Component) else component()
  323. except exceptions.MatchTypeError:
  324. raise
  325. except TypeError as e:
  326. message = str(e)
  327. if "BaseVar" in message or "ComputedVar" in message:
  328. raise TypeError(
  329. "You may be trying to use an invalid Python function on a state var. "
  330. "When referencing a var inside your render code, only limited var operations are supported. "
  331. "See the var operation docs here: https://reflex.dev/docs/vars/var-operations/"
  332. ) from e
  333. raise e
  334. def add_page(
  335. self,
  336. component: Component | ComponentCallable,
  337. route: str | None = None,
  338. title: str | None = None,
  339. description: str | None = None,
  340. image: str = constants.DefaultPage.IMAGE,
  341. on_load: (
  342. EventHandler | EventSpec | list[EventHandler | EventSpec] | None
  343. ) = None,
  344. meta: list[dict[str, str]] = constants.DefaultPage.META_LIST,
  345. script_tags: list[Component] | None = None,
  346. ):
  347. """Add a page to the app.
  348. If the component is a callable, by default the route is the name of the
  349. function. Otherwise, a route must be provided.
  350. Args:
  351. component: The component to display at the page.
  352. route: The route to display the component at.
  353. title: The title of the page.
  354. description: The description of the page.
  355. image: The image to display on the page.
  356. on_load: The event handler(s) that will be called each time the page load.
  357. meta: The metadata of the page.
  358. script_tags: List of script tags to be added to component
  359. """
  360. # If the route is not set, get it from the callable.
  361. if route is None:
  362. assert isinstance(
  363. component, Callable
  364. ), "Route must be set if component is not a callable."
  365. # Format the route.
  366. route = format.format_route(component.__name__)
  367. else:
  368. route = format.format_route(route, format_case=False)
  369. # Check if the route given is valid
  370. verify_route_validity(route)
  371. # Setup dynamic args for the route.
  372. # this state assignment is only required for tests using the deprecated state kwarg for App
  373. state = self.state if self.state else State
  374. state.setup_dynamic_args(get_route_args(route))
  375. # Generate the component if it is a callable.
  376. component = self._generate_component(component)
  377. # Ensure state is enabled if this page uses state.
  378. if self.state is None:
  379. if on_load or component._has_event_triggers():
  380. self.enable_state()
  381. else:
  382. for var in component._get_vars(include_children=True):
  383. if not var._var_data:
  384. continue
  385. if not var._var_data.state:
  386. continue
  387. self.enable_state()
  388. break
  389. component = OverlayFragment.create(component)
  390. meta_args = {
  391. "title": (
  392. title
  393. if title is not None
  394. else format.make_default_page_title(get_config().app_name, route)
  395. ),
  396. "image": image,
  397. "meta": meta,
  398. }
  399. if description is not None:
  400. meta_args["description"] = description
  401. # Add meta information to the component.
  402. compiler_utils.add_meta(
  403. component,
  404. **meta_args,
  405. )
  406. # Add script tags if given
  407. if script_tags:
  408. console.deprecate(
  409. feature_name="Passing script tags to add_page",
  410. reason="Add script components as children to the page component instead",
  411. deprecation_version="0.2.9",
  412. removal_version="0.5.0",
  413. )
  414. component.children.extend(script_tags)
  415. # Add the page.
  416. self._check_routes_conflict(route)
  417. self.pages[route] = component
  418. # Add the load events.
  419. if on_load:
  420. if not isinstance(on_load, list):
  421. on_load = [on_load]
  422. self.load_events[route] = on_load
  423. def get_load_events(self, route: str) -> list[EventHandler | EventSpec]:
  424. """Get the load events for a route.
  425. Args:
  426. route: The route to get the load events for.
  427. Returns:
  428. The load events for the route.
  429. """
  430. route = route.lstrip("/")
  431. if route == "":
  432. route = constants.PageNames.INDEX_ROUTE
  433. return self.load_events.get(route, [])
  434. def _check_routes_conflict(self, new_route: str):
  435. """Verify if there is any conflict between the new route and any existing route.
  436. Based on conflicts that NextJS would throw if not intercepted.
  437. Raises:
  438. ValueError: exception showing which conflict exist with the route to be added
  439. Args:
  440. new_route: the route being newly added.
  441. """
  442. newroute_catchall = catchall_in_route(new_route)
  443. if not newroute_catchall:
  444. return
  445. for route in self.pages:
  446. route = "" if route == "index" else route
  447. if new_route.startswith(f"{route}/[[..."):
  448. raise ValueError(
  449. f"You cannot define a route with the same specificity as a optional catch-all route ('{route}' and '{new_route}')"
  450. )
  451. route_catchall = catchall_in_route(route)
  452. if (
  453. route_catchall
  454. and newroute_catchall
  455. and catchall_prefix(route) == catchall_prefix(new_route)
  456. ):
  457. raise ValueError(
  458. f"You cannot use multiple catchall for the same dynamic route ({route} !== {new_route})"
  459. )
  460. def add_custom_404_page(
  461. self,
  462. component: Component | ComponentCallable | None = None,
  463. title: str = constants.Page404.TITLE,
  464. image: str = constants.Page404.IMAGE,
  465. description: str = constants.Page404.DESCRIPTION,
  466. on_load: (
  467. EventHandler | EventSpec | list[EventHandler | EventSpec] | None
  468. ) = None,
  469. meta: list[dict[str, str]] = constants.DefaultPage.META_LIST,
  470. ):
  471. """Define a custom 404 page for any url having no match.
  472. If there is no page defined on 'index' route, add the 404 page to it.
  473. If there is no global catchall defined, add the 404 page with a catchall
  474. Args:
  475. component: The component to display at the page.
  476. title: The title of the page.
  477. description: The description of the page.
  478. image: The image to display on the page.
  479. on_load: The event handler(s) that will be called each time the page load.
  480. meta: The metadata of the page.
  481. """
  482. if component is None:
  483. component = Default404Page.create()
  484. self.add_page(
  485. component=wait_for_client_redirect(self._generate_component(component)),
  486. route=constants.Page404.SLUG,
  487. title=title or constants.Page404.TITLE,
  488. image=image or constants.Page404.IMAGE,
  489. description=description or constants.Page404.DESCRIPTION,
  490. on_load=on_load,
  491. meta=meta,
  492. )
  493. def setup_admin_dash(self):
  494. """Setup the admin dash."""
  495. # Get the admin dash.
  496. admin_dash = self.admin_dash
  497. if admin_dash and admin_dash.models:
  498. # Build the admin dashboard
  499. admin = (
  500. admin_dash.admin
  501. if admin_dash.admin
  502. else Admin(
  503. engine=Model.get_db_engine(),
  504. title="Reflex Admin Dashboard",
  505. logo_url="https://reflex.dev/Reflex.svg",
  506. )
  507. )
  508. for model in admin_dash.models:
  509. view = admin_dash.view_overrides.get(model, ModelView)
  510. admin.add_view(view(model))
  511. admin.mount_to(self.api)
  512. def get_frontend_packages(self, imports: Dict[str, set[ImportVar]]):
  513. """Gets the frontend packages to be installed and filters out the unnecessary ones.
  514. Args:
  515. imports: A dictionary containing the imports used in the current page.
  516. Example:
  517. >>> get_frontend_packages({"react": "16.14.0", "react-dom": "16.14.0"})
  518. """
  519. page_imports = {
  520. i
  521. for i, tags in imports.items()
  522. if i
  523. not in [
  524. *constants.PackageJson.DEPENDENCIES.keys(),
  525. *constants.PackageJson.DEV_DEPENDENCIES.keys(),
  526. ]
  527. and not any(i.startswith(prefix) for prefix in ["/", ".", "next/"])
  528. and i != ""
  529. and any(tag.install for tag in tags)
  530. }
  531. frontend_packages = get_config().frontend_packages
  532. _frontend_packages = []
  533. for package in frontend_packages:
  534. if package in (get_config().tailwind or {}).get("plugins", []): # type: ignore
  535. console.warn(
  536. f"Tailwind packages are inferred from 'plugins', remove `{package}` from `frontend_packages`"
  537. )
  538. continue
  539. if package in page_imports:
  540. console.warn(
  541. f"React packages and their dependencies are inferred from Component.library and Component.lib_dependencies, remove `{package}` from `frontend_packages`"
  542. )
  543. continue
  544. _frontend_packages.append(package)
  545. page_imports.update(_frontend_packages)
  546. prerequisites.install_frontend_packages(page_imports, get_config())
  547. def _app_root(self, app_wrappers: dict[tuple[int, str], Component]) -> Component:
  548. for component in tuple(app_wrappers.values()):
  549. app_wrappers.update(component.get_app_wrap_components())
  550. order = sorted(app_wrappers, key=lambda k: k[0], reverse=True)
  551. root = parent = copy.deepcopy(app_wrappers[order[0]])
  552. for key in order[1:]:
  553. child = copy.deepcopy(app_wrappers[key])
  554. parent.children.append(child)
  555. parent = child
  556. return root
  557. def _should_compile(self) -> bool:
  558. """Check if the app should be compiled.
  559. Returns:
  560. Whether the app should be compiled.
  561. """
  562. # Check the environment variable.
  563. if should_skip_compile():
  564. return False
  565. # Check the nocompile file.
  566. if os.path.exists(constants.NOCOMPILE_FILE):
  567. # Delete the nocompile file
  568. os.remove(constants.NOCOMPILE_FILE)
  569. return False
  570. # By default, compile the app.
  571. return True
  572. def _add_overlay_to_component(self, component: Component) -> Component:
  573. if self.overlay_component is None:
  574. return component
  575. children = component.children
  576. overlay_component = self._generate_component(self.overlay_component)
  577. if children[0] == overlay_component:
  578. return component
  579. # recreate OverlayFragment with overlay_component as first child
  580. component = OverlayFragment.create(overlay_component, *children)
  581. return component
  582. def _setup_overlay_component(self):
  583. """If a State is not used and no overlay_component is specified, do not render the connection modal."""
  584. if self.state is None and self.overlay_component is default_overlay_component:
  585. self.overlay_component = None
  586. for k, component in self.pages.items():
  587. self.pages[k] = self._add_overlay_to_component(component)
  588. def compile(self):
  589. """compile_() is the new function for performing compilation.
  590. Reflex framework will call it automatically as needed.
  591. """
  592. console.deprecate(
  593. feature_name="app.compile()",
  594. reason="Explicit calls to app.compile() are not needed."
  595. " Method will be removed in 0.4.0",
  596. deprecation_version="0.3.8",
  597. removal_version="0.5.0",
  598. )
  599. return
  600. def _apply_decorated_pages(self):
  601. """Add @rx.page decorated pages to the app.
  602. This has to be done in the MainThread for py38 and py39 compatibility, so the
  603. decorated pages are added to the app before the app is compiled (in a thread)
  604. to workaround REF-2172.
  605. This can move back into `compile_` when py39 support is dropped.
  606. """
  607. # Add the @rx.page decorated pages to collect on_load events.
  608. for render, kwargs in DECORATED_PAGES:
  609. self.add_page(render, **kwargs)
  610. def compile_(self):
  611. """Compile the app and output it to the pages folder.
  612. Raises:
  613. RuntimeError: When any page uses state, but no rx.State subclass is defined.
  614. """
  615. # Render a default 404 page if the user didn't supply one
  616. if constants.Page404.SLUG not in self.pages:
  617. self.add_custom_404_page()
  618. # Add the optional endpoints (_upload)
  619. self.add_optional_endpoints()
  620. if not self._should_compile():
  621. return
  622. self._setup_overlay_component()
  623. # Create a progress bar.
  624. progress = Progress(
  625. *Progress.get_default_columns()[:-1],
  626. MofNCompleteColumn(),
  627. TimeElapsedColumn(),
  628. )
  629. # try to be somewhat accurate - but still not 100%
  630. adhoc_steps_without_executor = 6
  631. fixed_pages_within_executor = 5
  632. progress.start()
  633. task = progress.add_task(
  634. "Compiling:",
  635. total=len(self.pages)
  636. + fixed_pages_within_executor
  637. + adhoc_steps_without_executor,
  638. )
  639. # Get the env mode.
  640. config = get_config()
  641. # Store the compile results.
  642. compile_results = []
  643. # Add the app wrappers.
  644. app_wrappers: Dict[tuple[int, str], Component] = {
  645. # Default app wrap component renders {children}
  646. (0, "AppWrap"): AppWrap.create()
  647. }
  648. if self.theme is not None:
  649. # If a theme component was provided, wrap the app with it
  650. app_wrappers[(20, "Theme")] = self.theme
  651. progress.advance(task)
  652. # Fix up the style.
  653. self.style = evaluate_style_namespaces(self.style)
  654. # Track imports and custom components found.
  655. all_imports = {}
  656. custom_components = set()
  657. for _route, component in self.pages.items():
  658. # Merge the component style with the app style.
  659. component.add_style(self.style)
  660. component.apply_theme(self.theme)
  661. # Add component.get_imports() to all_imports.
  662. all_imports.update(component.get_imports())
  663. # Add the app wrappers from this component.
  664. app_wrappers.update(component.get_app_wrap_components())
  665. # Add the custom components from the page to the set.
  666. custom_components |= component.get_custom_components()
  667. progress.advance(task)
  668. # Perform auto-memoization of stateful components.
  669. (
  670. stateful_components_path,
  671. stateful_components_code,
  672. page_components,
  673. ) = compiler.compile_stateful_components(self.pages.values())
  674. progress.advance(task)
  675. # Catch "static" apps (that do not define a rx.State subclass) which are trying to access rx.State.
  676. if code_uses_state_contexts(stateful_components_code) and self.state is None:
  677. raise RuntimeError(
  678. "To access rx.State in frontend components, at least one "
  679. "subclass of rx.State must be defined in the app."
  680. )
  681. compile_results.append((stateful_components_path, stateful_components_code))
  682. # Compile the root document before fork.
  683. compile_results.append(
  684. compiler.compile_document_root(
  685. self.head_components,
  686. html_lang=self.html_lang,
  687. html_custom_attrs=self.html_custom_attrs, # type: ignore
  688. )
  689. )
  690. # Compile the contexts before fork.
  691. compile_results.append(
  692. compiler.compile_contexts(self.state, self.theme),
  693. )
  694. app_root = self._app_root(app_wrappers=app_wrappers)
  695. progress.advance(task)
  696. # Prepopulate the global ExecutorSafeFunctions class with input data required by the compile functions.
  697. # This is required for multiprocessing to work, in presence of non-picklable inputs.
  698. for route, component in zip(self.pages, page_components):
  699. ExecutorSafeFunctions.COMPILE_PAGE_ARGS_BY_ROUTE[route] = (
  700. route,
  701. component,
  702. self.state,
  703. )
  704. ExecutorSafeFunctions.COMPILE_APP_APP_ROOT = app_root
  705. ExecutorSafeFunctions.CUSTOM_COMPONENTS = custom_components
  706. ExecutorSafeFunctions.STYLE = self.style
  707. # Use a forking process pool, if possible. Much faster, especially for large sites.
  708. # Fallback to ThreadPoolExecutor as something that will always work.
  709. executor = None
  710. if (
  711. platform.system() in ("Linux", "Darwin")
  712. and os.environ.get("REFLEX_COMPILE_PROCESSES") is not None
  713. ):
  714. executor = concurrent.futures.ProcessPoolExecutor(
  715. max_workers=int(os.environ.get("REFLEX_COMPILE_PROCESSES", 0)) or None,
  716. mp_context=multiprocessing.get_context("fork"),
  717. )
  718. else:
  719. executor = concurrent.futures.ThreadPoolExecutor(
  720. max_workers=int(os.environ.get("REFLEX_COMPILE_THREADS", 0)) or None,
  721. )
  722. with executor:
  723. result_futures = []
  724. custom_components_future = None
  725. def _mark_complete(_=None):
  726. progress.advance(task)
  727. def _submit_work(fn, *args, **kwargs):
  728. f = executor.submit(fn, *args, **kwargs)
  729. f.add_done_callback(_mark_complete)
  730. result_futures.append(f)
  731. # Compile all page components.
  732. for route in self.pages:
  733. _submit_work(ExecutorSafeFunctions.compile_page, route)
  734. # Compile the app wrapper.
  735. _submit_work(ExecutorSafeFunctions.compile_app)
  736. # Compile the custom components.
  737. custom_components_future = executor.submit(
  738. ExecutorSafeFunctions.compile_custom_components,
  739. )
  740. custom_components_future.add_done_callback(_mark_complete)
  741. # Compile the root stylesheet with base styles.
  742. _submit_work(compiler.compile_root_stylesheet, self.stylesheets)
  743. # Compile the theme.
  744. _submit_work(ExecutorSafeFunctions.compile_theme)
  745. # Compile the Tailwind config.
  746. if config.tailwind is not None:
  747. config.tailwind["content"] = config.tailwind.get(
  748. "content", constants.Tailwind.CONTENT
  749. )
  750. _submit_work(compiler.compile_tailwind, config.tailwind)
  751. else:
  752. _submit_work(compiler.remove_tailwind_from_postcss)
  753. # Wait for all compilation tasks to complete.
  754. for future in concurrent.futures.as_completed(result_futures):
  755. compile_results.append(future.result())
  756. # Special case for custom_components, since we need the compiled imports
  757. # to install proper frontend packages.
  758. (
  759. *custom_components_result,
  760. custom_components_imports,
  761. ) = custom_components_future.result()
  762. compile_results.append(custom_components_result)
  763. all_imports.update(custom_components_imports)
  764. # Get imports from AppWrap components.
  765. all_imports.update(app_root.get_imports())
  766. progress.advance(task)
  767. # Empty the .web pages directory.
  768. compiler.purge_web_pages_dir()
  769. progress.advance(task)
  770. progress.stop()
  771. # Install frontend packages.
  772. self.get_frontend_packages(all_imports)
  773. for output_path, code in compile_results:
  774. compiler_utils.write_page(output_path, code)
  775. @contextlib.asynccontextmanager
  776. async def modify_state(self, token: str) -> AsyncIterator[BaseState]:
  777. """Modify the state out of band.
  778. Args:
  779. token: The token to modify the state for.
  780. Yields:
  781. The state to modify.
  782. Raises:
  783. RuntimeError: If the app has not been initialized yet.
  784. """
  785. if self.event_namespace is None:
  786. raise RuntimeError("App has not been initialized yet.")
  787. # Get exclusive access to the state.
  788. async with self.state_manager.modify_state(token) as state:
  789. # No other event handler can modify the state while in this context.
  790. yield state
  791. delta = state.get_delta()
  792. if delta:
  793. # When the state is modified reset dirty status and emit the delta to the frontend.
  794. state._clean()
  795. await self.event_namespace.emit_update(
  796. update=StateUpdate(delta=delta),
  797. sid=state.router.session.session_id,
  798. )
  799. def _process_background(
  800. self, state: BaseState, event: Event
  801. ) -> asyncio.Task | None:
  802. """Process an event in the background and emit updates as they arrive.
  803. Args:
  804. state: The state to process the event for.
  805. event: The event to process.
  806. Returns:
  807. Task if the event was backgroundable, otherwise None
  808. """
  809. substate, handler = state._get_event_handler(event)
  810. if not handler.is_background:
  811. return None
  812. async def _coro():
  813. """Coroutine to process the event and emit updates inside an asyncio.Task.
  814. Raises:
  815. RuntimeError: If the app has not been initialized yet.
  816. """
  817. if self.event_namespace is None:
  818. raise RuntimeError("App has not been initialized yet.")
  819. # Process the event.
  820. async for update in state._process_event(
  821. handler=handler, state=substate, payload=event.payload
  822. ):
  823. # Postprocess the event.
  824. update = await self.postprocess(state, event, update)
  825. # Send the update to the client.
  826. await self.event_namespace.emit_update(
  827. update=update,
  828. sid=state.router.session.session_id,
  829. )
  830. task = asyncio.create_task(_coro())
  831. self.background_tasks.add(task)
  832. # Clean up task from background_tasks set when complete.
  833. task.add_done_callback(self.background_tasks.discard)
  834. return task
  835. async def process(
  836. app: App, event: Event, sid: str, headers: Dict, client_ip: str
  837. ) -> AsyncIterator[StateUpdate]:
  838. """Process an event.
  839. Args:
  840. app: The app to process the event for.
  841. event: The event to process.
  842. sid: The Socket.IO session id.
  843. headers: The client headers.
  844. client_ip: The client_ip.
  845. Yields:
  846. The state updates after processing the event.
  847. """
  848. # Add request data to the state.
  849. router_data = event.router_data
  850. router_data.update(
  851. {
  852. constants.RouteVar.QUERY: format.format_query_params(event.router_data),
  853. constants.RouteVar.CLIENT_TOKEN: event.token,
  854. constants.RouteVar.SESSION_ID: sid,
  855. constants.RouteVar.HEADERS: headers,
  856. constants.RouteVar.CLIENT_IP: client_ip,
  857. }
  858. )
  859. # Get the state for the session exclusively.
  860. async with app.state_manager.modify_state(event.substate_token) as state:
  861. # re-assign only when the value is different
  862. if state.router_data != router_data:
  863. # assignment will recurse into substates and force recalculation of
  864. # dependent ComputedVar (dynamic route variables)
  865. state.router_data = router_data
  866. state.router = RouterData(router_data)
  867. # Preprocess the event.
  868. update = await app.preprocess(state, event)
  869. # If there was an update, yield it.
  870. if update is not None:
  871. yield update
  872. # Only process the event if there is no update.
  873. else:
  874. if app._process_background(state, event) is not None:
  875. # `final=True` allows the frontend send more events immediately.
  876. yield StateUpdate(final=True)
  877. return
  878. # Process the event synchronously.
  879. async for update in state._process(event):
  880. # Postprocess the event.
  881. update = await app.postprocess(state, event, update)
  882. # Yield the update.
  883. yield update
  884. async def ping() -> str:
  885. """Test API endpoint.
  886. Returns:
  887. The response.
  888. """
  889. return "pong"
  890. def upload(app: App):
  891. """Upload a file.
  892. Args:
  893. app: The app to upload the file for.
  894. Returns:
  895. The upload function.
  896. """
  897. async def upload_file(request: Request, files: List[UploadFile]):
  898. """Upload a file.
  899. Args:
  900. request: The FastAPI request object.
  901. files: The file(s) to upload.
  902. Returns:
  903. StreamingResponse yielding newline-delimited JSON of StateUpdate
  904. emitted by the upload handler.
  905. Raises:
  906. ValueError: if there are no args with supported annotation.
  907. TypeError: if a background task is used as the handler.
  908. HTTPException: when the request does not include token / handler headers.
  909. """
  910. token = request.headers.get("reflex-client-token")
  911. handler = request.headers.get("reflex-event-handler")
  912. if not token or not handler:
  913. raise HTTPException(
  914. status_code=400,
  915. detail="Missing reflex-client-token or reflex-event-handler header.",
  916. )
  917. # Get the state for the session.
  918. substate_token = _substate_key(token, handler.rpartition(".")[0])
  919. state = await app.state_manager.get_state(substate_token)
  920. # get the current session ID
  921. # get the current state(parent state/substate)
  922. path = handler.split(".")[:-1]
  923. current_state = state.get_substate(path)
  924. handler_upload_param = ()
  925. # get handler function
  926. func = getattr(type(current_state), handler.split(".")[-1])
  927. # check if there exists any handler args with annotation, List[UploadFile]
  928. if isinstance(func, EventHandler):
  929. if func.is_background:
  930. raise TypeError(
  931. f"@rx.background is not supported for upload handler `{handler}`.",
  932. )
  933. func = func.fn
  934. if isinstance(func, functools.partial):
  935. func = func.func
  936. for k, v in get_type_hints(func).items():
  937. if types.is_generic_alias(v) and types._issubclass(
  938. get_args(v)[0],
  939. UploadFile,
  940. ):
  941. handler_upload_param = (k, v)
  942. break
  943. if not handler_upload_param:
  944. raise ValueError(
  945. f"`{handler}` handler should have a parameter annotated as "
  946. "List[rx.UploadFile]"
  947. )
  948. event = Event(
  949. token=token,
  950. name=handler,
  951. payload={handler_upload_param[0]: files},
  952. )
  953. async def _ndjson_updates():
  954. """Process the upload event, generating ndjson updates.
  955. Yields:
  956. Each state update as JSON followed by a new line.
  957. """
  958. # Process the event.
  959. async with app.state_manager.modify_state(event.substate_token) as state:
  960. async for update in state._process(event):
  961. # Postprocess the event.
  962. update = await app.postprocess(state, event, update)
  963. yield update.json() + "\n"
  964. # Stream updates to client
  965. return StreamingResponse(
  966. _ndjson_updates(),
  967. media_type="application/x-ndjson",
  968. )
  969. return upload_file
  970. class EventNamespace(AsyncNamespace):
  971. """The event namespace."""
  972. # The application object.
  973. app: App
  974. def __init__(self, namespace: str, app: App):
  975. """Initialize the event namespace.
  976. Args:
  977. namespace: The namespace.
  978. app: The application object.
  979. """
  980. super().__init__(namespace)
  981. self.app = app
  982. def on_connect(self, sid, environ):
  983. """Event for when the websocket is connected.
  984. Args:
  985. sid: The Socket.IO session id.
  986. environ: The request information, including HTTP headers.
  987. """
  988. pass
  989. def on_disconnect(self, sid):
  990. """Event for when the websocket disconnects.
  991. Args:
  992. sid: The Socket.IO session id.
  993. """
  994. pass
  995. async def emit_update(self, update: StateUpdate, sid: str) -> None:
  996. """Emit an update to the client.
  997. Args:
  998. update: The state update to send.
  999. sid: The Socket.IO session id.
  1000. """
  1001. # Creating a task prevents the update from being blocked behind other coroutines.
  1002. await asyncio.create_task(
  1003. self.emit(str(constants.SocketEvent.EVENT), update.json(), to=sid)
  1004. )
  1005. async def on_event(self, sid, data):
  1006. """Event for receiving front-end websocket events.
  1007. Args:
  1008. sid: The Socket.IO session id.
  1009. data: The event data.
  1010. """
  1011. # Get the event.
  1012. event = Event.parse_raw(data)
  1013. # Get the event environment.
  1014. assert self.app.sio is not None
  1015. environ = self.app.sio.get_environ(sid, self.namespace)
  1016. assert environ is not None
  1017. # Get the client headers.
  1018. headers = {
  1019. k.decode("utf-8"): v.decode("utf-8")
  1020. for (k, v) in environ["asgi.scope"]["headers"]
  1021. }
  1022. # Get the client IP
  1023. try:
  1024. client_ip = environ["asgi.scope"]["client"][0]
  1025. except (KeyError, IndexError):
  1026. client_ip = environ.get("REMOTE_ADDR", "0.0.0.0")
  1027. # Process the events.
  1028. async for update in process(self.app, event, sid, headers, client_ip):
  1029. # Emit the update from processing the event.
  1030. await self.emit_update(update=update, sid=sid)
  1031. async def on_ping(self, sid):
  1032. """Event for testing the API endpoint.
  1033. Args:
  1034. sid: The Socket.IO session id.
  1035. """
  1036. # Emit the test event.
  1037. await self.emit(str(constants.SocketEvent.PING), "pong", to=sid)