app.py 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  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 dataclasses
  8. import functools
  9. import inspect
  10. import io
  11. import json
  12. import sys
  13. import traceback
  14. from collections.abc import AsyncIterator, Callable, Coroutine, Sequence
  15. from datetime import datetime
  16. from pathlib import Path
  17. from timeit import default_timer as timer
  18. from types import SimpleNamespace
  19. from typing import TYPE_CHECKING, Any, BinaryIO, get_args, get_type_hints
  20. from fastapi import FastAPI
  21. from rich.progress import MofNCompleteColumn, Progress, TimeElapsedColumn
  22. from socketio import ASGIApp as EngineIOApp
  23. from socketio import AsyncNamespace, AsyncServer
  24. from starlette.applications import Starlette
  25. from starlette.datastructures import Headers
  26. from starlette.datastructures import UploadFile as StarletteUploadFile
  27. from starlette.exceptions import HTTPException
  28. from starlette.middleware import cors
  29. from starlette.requests import ClientDisconnect, Request
  30. from starlette.responses import JSONResponse, Response, StreamingResponse
  31. from starlette.staticfiles import StaticFiles
  32. from reflex import constants
  33. from reflex.admin import AdminDash
  34. from reflex.app_mixins import AppMixin, LifespanMixin, MiddlewareMixin
  35. from reflex.compiler import compiler
  36. from reflex.compiler import utils as compiler_utils
  37. from reflex.compiler.compiler import (
  38. ExecutorSafeFunctions,
  39. compile_theme,
  40. readable_name_from_component,
  41. )
  42. from reflex.components.base.app_wrap import AppWrap
  43. from reflex.components.base.error_boundary import ErrorBoundary
  44. from reflex.components.base.fragment import Fragment
  45. from reflex.components.base.strict_mode import StrictMode
  46. from reflex.components.component import (
  47. CUSTOM_COMPONENTS,
  48. Component,
  49. ComponentStyle,
  50. evaluate_style_namespaces,
  51. )
  52. from reflex.components.core.banner import (
  53. backend_disabled,
  54. connection_pulser,
  55. connection_toaster,
  56. )
  57. from reflex.components.core.breakpoints import set_breakpoints
  58. from reflex.components.core.client_side_routing import (
  59. Default404Page,
  60. wait_for_client_redirect,
  61. )
  62. from reflex.components.core.sticky import sticky
  63. from reflex.components.core.upload import Upload, get_upload_dir
  64. from reflex.components.radix import themes
  65. from reflex.components.sonner.toast import toast
  66. from reflex.config import ExecutorType, environment, get_config
  67. from reflex.event import (
  68. _EVENT_FIELDS,
  69. Event,
  70. EventHandler,
  71. EventSpec,
  72. EventType,
  73. IndividualEventType,
  74. get_hydrate_event,
  75. )
  76. from reflex.model import Model, get_db_status
  77. from reflex.page import DECORATED_PAGES
  78. from reflex.route import (
  79. get_route_args,
  80. replace_brackets_with_keywords,
  81. verify_route_validity,
  82. )
  83. from reflex.state import (
  84. BaseState,
  85. RouterData,
  86. State,
  87. StateManager,
  88. StateUpdate,
  89. _substate_key,
  90. all_base_state_classes,
  91. code_uses_state_contexts,
  92. )
  93. from reflex.utils import (
  94. codespaces,
  95. console,
  96. exceptions,
  97. format,
  98. path_ops,
  99. prerequisites,
  100. types,
  101. )
  102. from reflex.utils.exec import get_compile_context, is_prod_mode, is_testing_env
  103. from reflex.utils.imports import ImportVar
  104. from reflex.utils.types import ASGIApp, Message, Receive, Scope, Send
  105. if TYPE_CHECKING:
  106. from reflex.vars import Var
  107. # Define custom types.
  108. ComponentCallable = Callable[[], Component | tuple[Component, ...] | str | Var]
  109. else:
  110. ComponentCallable = Callable[[], Component | tuple[Component, ...] | str]
  111. Reducer = Callable[[Event], Coroutine[Any, Any, StateUpdate]]
  112. def default_frontend_exception_handler(exception: Exception) -> None:
  113. """Default frontend exception handler function.
  114. Args:
  115. exception: The exception.
  116. """
  117. console.error(f"[Reflex Frontend Exception]\n {exception}\n")
  118. def default_backend_exception_handler(exception: Exception) -> EventSpec:
  119. """Default backend exception handler function.
  120. Args:
  121. exception: The exception.
  122. Returns:
  123. EventSpec: The window alert event.
  124. """
  125. from reflex.components.sonner.toast import toast
  126. error = traceback.format_exc()
  127. console.error(f"[Reflex Backend Exception]\n {error}\n")
  128. error_message = (
  129. ["Contact the website administrator."]
  130. if is_prod_mode()
  131. else [f"{type(exception).__name__}: {exception}.", "See logs for details."]
  132. )
  133. return toast(
  134. "An error occurred.",
  135. level="error",
  136. fallback_to_alert=True,
  137. description="<br/>".join(error_message),
  138. position="top-center",
  139. id="backend_error",
  140. style={"width": "500px"},
  141. )
  142. def extra_overlay_function() -> Component | None:
  143. """Extra overlay function to add to the overlay component.
  144. Returns:
  145. The extra overlay function.
  146. """
  147. config = get_config()
  148. extra_config = config.extra_overlay_function
  149. config_overlay = None
  150. if extra_config:
  151. module, _, function_name = extra_config.rpartition(".")
  152. try:
  153. module = __import__(module)
  154. config_overlay = Fragment.create(getattr(module, function_name)())
  155. config_overlay._get_all_imports()
  156. except Exception as e:
  157. from reflex.compiler.utils import save_error
  158. log_path = save_error(e)
  159. console.error(
  160. f"Error loading extra_overlay_function {extra_config}. Error saved to {log_path}"
  161. )
  162. return config_overlay
  163. def default_overlay_component() -> Component:
  164. """Default overlay_component attribute for App.
  165. Returns:
  166. The default overlay_component, which is a connection_modal.
  167. """
  168. from reflex.components.component import memo
  169. def default_overlay_components():
  170. return Fragment.create(
  171. connection_pulser(),
  172. connection_toaster(),
  173. *(
  174. [backend_disabled()]
  175. if get_compile_context() == constants.CompileContext.DEPLOY
  176. else []
  177. ),
  178. *codespaces.codespaces_auto_redirect(),
  179. )
  180. return Fragment.create(memo(default_overlay_components)())
  181. def default_error_boundary(*children: Component) -> Component:
  182. """Default error_boundary attribute for App.
  183. Args:
  184. *children: The children to render in the error boundary.
  185. Returns:
  186. The default error_boundary, which is an ErrorBoundary.
  187. """
  188. return ErrorBoundary.create(*children)
  189. class OverlayFragment(Fragment):
  190. """Alias for Fragment, used to wrap the overlay_component."""
  191. pass
  192. @dataclasses.dataclass(frozen=True)
  193. class UploadFile(StarletteUploadFile):
  194. """A file uploaded to the server.
  195. Args:
  196. file: The standard Python file object (non-async).
  197. filename: The original file name.
  198. size: The size of the file in bytes.
  199. headers: The headers of the request.
  200. """
  201. file: BinaryIO
  202. path: Path | None = dataclasses.field(default=None)
  203. _deprecated_filename: str | None = dataclasses.field(default=None)
  204. size: int | None = dataclasses.field(default=None)
  205. headers: Headers = dataclasses.field(default_factory=Headers)
  206. @property
  207. def name(self) -> str | None:
  208. """Get the name of the uploaded file.
  209. Returns:
  210. The name of the uploaded file.
  211. """
  212. if self.path:
  213. return self.path.name
  214. @property
  215. def filename(self) -> str | None:
  216. """Get the filename of the uploaded file.
  217. Returns:
  218. The filename of the uploaded file.
  219. """
  220. console.deprecate(
  221. feature_name="UploadFile.filename",
  222. reason="Use UploadFile.name instead.",
  223. deprecation_version="0.7.1",
  224. removal_version="0.8.0",
  225. )
  226. return self._deprecated_filename
  227. @dataclasses.dataclass(
  228. frozen=True,
  229. )
  230. class UnevaluatedPage:
  231. """An uncompiled page."""
  232. component: Component | ComponentCallable
  233. route: str
  234. title: Var | str | None
  235. description: Var | str | None
  236. image: str
  237. on_load: EventType[()] | None
  238. meta: list[dict[str, str]]
  239. context: dict[str, Any] | None
  240. def merged_with(self, other: UnevaluatedPage) -> UnevaluatedPage:
  241. """Merge the other page into this one.
  242. Args:
  243. other: The other page to merge with.
  244. Returns:
  245. The merged page.
  246. """
  247. return dataclasses.replace(
  248. self,
  249. title=self.title if self.title is not None else other.title,
  250. description=self.description
  251. if self.description is not None
  252. else other.description,
  253. on_load=self.on_load if self.on_load is not None else other.on_load,
  254. context=self.context if self.context is not None else other.context,
  255. )
  256. @dataclasses.dataclass()
  257. class App(MiddlewareMixin, LifespanMixin):
  258. """The main Reflex app that encapsulates the backend and frontend.
  259. Every Reflex app needs an app defined in its main module.
  260. ```python
  261. # app.py
  262. import reflex as rx
  263. # Define state and pages
  264. ...
  265. app = rx.App(
  266. # Set global level style.
  267. style={...},
  268. # Set the top level theme.
  269. theme=rx.theme(accent_color="blue"),
  270. )
  271. ```
  272. """
  273. # The global [theme](https://reflex.dev/docs/styling/theming/#theme) for the entire app.
  274. theme: Component | None = dataclasses.field(
  275. default_factory=lambda: themes.theme(accent_color="blue")
  276. )
  277. # The [global style](https://reflex.dev/docs/styling/overview/#global-styles}) for the app.
  278. style: ComponentStyle = dataclasses.field(default_factory=dict)
  279. # A list of URLs to [stylesheets](https://reflex.dev/docs/styling/custom-stylesheets/) to include in the app.
  280. stylesheets: list[str] = dataclasses.field(default_factory=list)
  281. # A component that is present on every page (defaults to the Connection Error banner).
  282. overlay_component: Component | ComponentCallable | None = dataclasses.field(
  283. default=None
  284. )
  285. # Error boundary component to wrap the app with.
  286. error_boundary: ComponentCallable | None = dataclasses.field(default=None)
  287. # App wraps to be applied to the whole app. Expected to be a dictionary of (order, name) to a function that takes whether the state is enabled and optionally returns a component.
  288. app_wraps: dict[tuple[int, str], Callable[[bool], Component | None]] = (
  289. dataclasses.field(
  290. default_factory=lambda: {
  291. (55, "ErrorBoundary"): (
  292. lambda stateful: default_error_boundary() if stateful else None
  293. ),
  294. (5, "Overlay"): (
  295. lambda stateful: default_overlay_component() if stateful else None
  296. ),
  297. (4, "ExtraOverlay"): lambda stateful: extra_overlay_function(),
  298. }
  299. )
  300. )
  301. # Components to add to the head of every page.
  302. head_components: list[Component] = dataclasses.field(default_factory=list)
  303. # The Socket.IO AsyncServer instance.
  304. sio: AsyncServer | None = None
  305. # The language to add to the html root tag of every page.
  306. html_lang: str | None = None
  307. # Attributes to add to the html root tag of every page.
  308. html_custom_attrs: dict[str, str] | None = None
  309. # A map from a route to an unevaluated page.
  310. _unevaluated_pages: dict[str, UnevaluatedPage] = dataclasses.field(
  311. default_factory=dict
  312. )
  313. # A map from a page route to the component to render. Users should use `add_page`.
  314. _pages: dict[str, Component] = dataclasses.field(default_factory=dict)
  315. # A mapping of pages which created states as they were being evaluated.
  316. _stateful_pages: dict[str, None] = dataclasses.field(default_factory=dict)
  317. # The backend API object.
  318. _api: FastAPI | None = None
  319. # The state class to use for the app.
  320. _state: type[BaseState] | None = None
  321. # Class to manage many client states.
  322. _state_manager: StateManager | None = None
  323. # Mapping from a route to event handlers to trigger when the page loads.
  324. _load_events: dict[str, list[IndividualEventType[()]]] = dataclasses.field(
  325. default_factory=dict
  326. )
  327. # Admin dashboard to view and manage the database.
  328. admin_dash: AdminDash | None = None
  329. # The async server name space.
  330. _event_namespace: EventNamespace | None = None
  331. # Background tasks that are currently running.
  332. _background_tasks: set[asyncio.Task] = dataclasses.field(default_factory=set)
  333. # Frontend Error Handler Function
  334. frontend_exception_handler: Callable[[Exception], None] = (
  335. default_frontend_exception_handler
  336. )
  337. # Backend Error Handler Function
  338. backend_exception_handler: Callable[
  339. [Exception], EventSpec | list[EventSpec] | None
  340. ] = default_backend_exception_handler
  341. # Put the toast provider in the app wrap.
  342. toaster: Component | None = dataclasses.field(default_factory=toast.provider)
  343. # Transform the ASGI app before running it.
  344. api_transformer: (
  345. Sequence[Callable[[ASGIApp], ASGIApp] | Starlette]
  346. | Callable[[ASGIApp], ASGIApp]
  347. | Starlette
  348. | None
  349. ) = None
  350. @property
  351. def api(self) -> FastAPI:
  352. """Get the backend api.
  353. Returns:
  354. The backend api.
  355. Raises:
  356. ValueError: If the app has not been initialized.
  357. """
  358. if self._api is None:
  359. raise ValueError("The app has not been initialized.")
  360. return self._api
  361. @property
  362. def event_namespace(self) -> EventNamespace | None:
  363. """Get the event namespace.
  364. Returns:
  365. The event namespace.
  366. """
  367. return self._event_namespace
  368. def __post_init__(self):
  369. """Initialize the app.
  370. Raises:
  371. ValueError: If the event namespace is not provided in the config.
  372. Also, if there are multiple client subclasses of rx.BaseState(Subclasses of rx.BaseState should consist
  373. of the DefaultState and the client app state).
  374. """
  375. # Special case to allow test cases have multiple subclasses of rx.BaseState.
  376. if not is_testing_env() and BaseState.__subclasses__() != [State]:
  377. # Only rx.State is allowed as Base State subclass.
  378. raise ValueError(
  379. "rx.BaseState cannot be subclassed directly. Use rx.State instead"
  380. )
  381. get_config(reload=True)
  382. if "breakpoints" in self.style:
  383. set_breakpoints(self.style.pop("breakpoints"))
  384. # Set up the API.
  385. self._api = FastAPI(lifespan=self._run_lifespan_tasks)
  386. App._add_cors(self._api)
  387. self._add_default_endpoints()
  388. for clz in App.__mro__:
  389. if clz == App:
  390. continue
  391. if issubclass(clz, AppMixin):
  392. clz._init_mixin(self)
  393. self._setup_state()
  394. # Set up the admin dash.
  395. self._setup_admin_dash()
  396. if sys.platform == "win32" and not is_prod_mode():
  397. # Hack to fix Windows hot reload issue.
  398. from reflex.utils.compat import windows_hot_reload_lifespan_hack
  399. self.register_lifespan_task(windows_hot_reload_lifespan_hack)
  400. def _enable_state(self) -> None:
  401. """Enable state for the app."""
  402. if not self._state:
  403. self._state = State
  404. self._setup_state()
  405. def _setup_state(self) -> None:
  406. """Set up the state for the app.
  407. Raises:
  408. RuntimeError: If the socket server is invalid.
  409. """
  410. if not self._state:
  411. return
  412. config = get_config()
  413. # Set up the state manager.
  414. self._state_manager = StateManager.create(state=self._state)
  415. # Set up the Socket.IO AsyncServer.
  416. if not self.sio:
  417. self.sio = AsyncServer(
  418. async_mode="asgi",
  419. cors_allowed_origins=(
  420. "*"
  421. if config.cors_allowed_origins == ["*"]
  422. else config.cors_allowed_origins
  423. ),
  424. cors_credentials=True,
  425. max_http_buffer_size=environment.REFLEX_SOCKET_MAX_HTTP_BUFFER_SIZE.get(),
  426. ping_interval=environment.REFLEX_SOCKET_INTERVAL.get(),
  427. ping_timeout=environment.REFLEX_SOCKET_TIMEOUT.get(),
  428. json=SimpleNamespace(
  429. dumps=staticmethod(format.json_dumps),
  430. loads=staticmethod(json.loads),
  431. ),
  432. transports=["websocket"],
  433. )
  434. elif getattr(self.sio, "async_mode", "") != "asgi":
  435. raise RuntimeError(
  436. f"Custom `sio` must use `async_mode='asgi'`, not '{self.sio.async_mode}'."
  437. )
  438. # Create the socket app. Note event endpoint constant replaces the default 'socket.io' path.
  439. socket_app = EngineIOApp(self.sio, socketio_path="")
  440. namespace = config.get_event_namespace()
  441. # Create the event namespace and attach the main app. Not related to any paths.
  442. self._event_namespace = EventNamespace(namespace, self)
  443. # Register the event namespace with the socket.
  444. self.sio.register_namespace(self.event_namespace)
  445. # Mount the socket app with the API.
  446. if self._api:
  447. class HeaderMiddleware:
  448. def __init__(self, app: ASGIApp):
  449. self.app = app
  450. async def __call__(self, scope: Scope, receive: Receive, send: Send):
  451. original_send = send
  452. async def modified_send(message: Message):
  453. if message["type"] == "websocket.accept":
  454. if scope.get("subprotocols"):
  455. # The following *does* say "subprotocol" instead of "subprotocols", intentionally.
  456. message["subprotocol"] = scope["subprotocols"][0]
  457. headers = dict(message.get("headers", []))
  458. header_key = b"sec-websocket-protocol"
  459. if subprotocol := headers.get(header_key):
  460. message["headers"] = [
  461. *message.get("headers", []),
  462. (header_key, subprotocol),
  463. ]
  464. return await original_send(message)
  465. return await self.app(scope, receive, modified_send)
  466. socket_app_with_headers = HeaderMiddleware(socket_app)
  467. self._api.mount(str(constants.Endpoint.EVENT), socket_app_with_headers)
  468. # Check the exception handlers
  469. self._validate_exception_handlers()
  470. def __repr__(self) -> str:
  471. """Get the string representation of the app.
  472. Returns:
  473. The string representation of the app.
  474. """
  475. return f"<App state={self._state.__name__ if self._state else None}>"
  476. def __call__(self) -> ASGIApp:
  477. """Run the backend api instance.
  478. Raises:
  479. ValueError: If the app has not been initialized.
  480. Returns:
  481. The backend api.
  482. """
  483. # For py3.9 compatibility when redis is used, we MUST add any decorator pages
  484. # before compiling the app in a thread to avoid event loop error (REF-2172).
  485. self._apply_decorated_pages()
  486. compile_future = concurrent.futures.ThreadPoolExecutor(max_workers=1).submit(
  487. self._compile
  488. )
  489. compile_future.add_done_callback(
  490. # Force background compile errors to print eagerly
  491. lambda f: f.result()
  492. )
  493. # Wait for the compile to finish to ensure all optional endpoints are mounted.
  494. compile_future.result()
  495. if not self._api:
  496. raise ValueError("The app has not been initialized.")
  497. asgi_app = self._api
  498. if self.api_transformer is not None:
  499. api_transformers: Sequence[Starlette | Callable[[ASGIApp], ASGIApp]] = (
  500. [self.api_transformer]
  501. if not isinstance(self.api_transformer, Sequence)
  502. else self.api_transformer
  503. )
  504. for api_transformer in api_transformers:
  505. if isinstance(api_transformer, Starlette):
  506. # Mount the api to the fastapi app.
  507. App._add_cors(api_transformer)
  508. api_transformer.mount("", asgi_app)
  509. asgi_app = api_transformer
  510. else:
  511. # Transform the asgi app.
  512. asgi_app = api_transformer(asgi_app)
  513. return asgi_app
  514. def _add_default_endpoints(self):
  515. """Add default api endpoints (ping)."""
  516. # To test the server.
  517. if not self._api:
  518. return
  519. self._api.add_route(
  520. str(constants.Endpoint.PING),
  521. ping,
  522. methods=["GET"],
  523. )
  524. self._api.add_route(
  525. str(constants.Endpoint.HEALTH),
  526. health,
  527. methods=["GET"],
  528. )
  529. def _add_optional_endpoints(self):
  530. """Add optional api endpoints (_upload)."""
  531. if not self._api:
  532. return
  533. upload_is_used_marker = (
  534. prerequisites.get_backend_dir() / constants.Dirs.UPLOAD_IS_USED
  535. )
  536. if Upload.is_used or upload_is_used_marker.exists():
  537. # To upload files.
  538. self._api.add_route(
  539. str(constants.Endpoint.UPLOAD),
  540. upload(self),
  541. methods=["POST"],
  542. )
  543. # To access uploaded files.
  544. self._api.mount(
  545. str(constants.Endpoint.UPLOAD),
  546. StaticFiles(directory=get_upload_dir()),
  547. name="uploaded_files",
  548. )
  549. upload_is_used_marker.parent.mkdir(parents=True, exist_ok=True)
  550. upload_is_used_marker.touch()
  551. if codespaces.is_running_in_codespaces():
  552. self._api.add_route(
  553. str(constants.Endpoint.AUTH_CODESPACE),
  554. codespaces.auth_codespace,
  555. methods=["GET"],
  556. )
  557. if environment.REFLEX_ADD_ALL_ROUTES_ENDPOINT.get():
  558. self.add_all_routes_endpoint()
  559. @staticmethod
  560. def _add_cors(api: Starlette):
  561. """Add CORS middleware to the app.
  562. Args:
  563. api: The Starlette app to add CORS middleware to.
  564. """
  565. api.add_middleware(
  566. cors.CORSMiddleware,
  567. allow_credentials=True,
  568. allow_methods=["*"],
  569. allow_headers=["*"],
  570. allow_origins=get_config().cors_allowed_origins,
  571. )
  572. @property
  573. def state_manager(self) -> StateManager:
  574. """Get the state manager.
  575. Returns:
  576. The initialized state manager.
  577. Raises:
  578. ValueError: if the state has not been initialized.
  579. """
  580. if self._state_manager is None:
  581. raise ValueError("The state manager has not been initialized.")
  582. return self._state_manager
  583. @staticmethod
  584. def _generate_component(component: Component | ComponentCallable) -> Component:
  585. """Generate a component from a callable.
  586. Args:
  587. component: The component function to call or Component to return as-is.
  588. Returns:
  589. The generated component.
  590. """
  591. from reflex.compiler.compiler import into_component
  592. return into_component(component)
  593. def add_page(
  594. self,
  595. component: Component | ComponentCallable | None = None,
  596. route: str | None = None,
  597. title: str | Var | None = None,
  598. description: str | Var | None = None,
  599. image: str = constants.DefaultPage.IMAGE,
  600. on_load: EventType[()] | None = None,
  601. meta: list[dict[str, str]] = constants.DefaultPage.META_LIST,
  602. context: dict[str, Any] | None = None,
  603. ):
  604. """Add a page to the app.
  605. If the component is a callable, by default the route is the name of the
  606. function. Otherwise, a route must be provided.
  607. Args:
  608. component: The component to display at the page.
  609. route: The route to display the component at.
  610. title: The title of the page.
  611. description: The description of the page.
  612. image: The image to display on the page.
  613. on_load: The event handler(s) that will be called each time the page load.
  614. meta: The metadata of the page.
  615. context: Values passed to page for custom page-specific logic.
  616. Raises:
  617. PageValueError: When the component is not set for a non-404 page.
  618. RouteValueError: When the specified route name already exists.
  619. """
  620. # If the route is not set, get it from the callable.
  621. if route is None:
  622. if not isinstance(component, Callable):
  623. raise exceptions.RouteValueError(
  624. "Route must be set if component is not a callable."
  625. )
  626. # Format the route.
  627. route = format.format_route(component.__name__)
  628. else:
  629. route = format.format_route(route, format_case=False)
  630. if route == constants.Page404.SLUG:
  631. if component is None:
  632. component = Default404Page.create()
  633. component = wait_for_client_redirect(self._generate_component(component))
  634. title = title or constants.Page404.TITLE
  635. description = description or constants.Page404.DESCRIPTION
  636. image = image or constants.Page404.IMAGE
  637. else:
  638. if component is None:
  639. raise exceptions.PageValueError(
  640. "Component must be set for a non-404 page."
  641. )
  642. # Check if the route given is valid
  643. verify_route_validity(route)
  644. unevaluated_page = UnevaluatedPage(
  645. component=component,
  646. route=route,
  647. title=title,
  648. description=description,
  649. image=image,
  650. on_load=on_load,
  651. meta=meta,
  652. context=context,
  653. )
  654. if route in self._unevaluated_pages:
  655. if self._unevaluated_pages[route].component is component:
  656. unevaluated_page = unevaluated_page.merged_with(
  657. self._unevaluated_pages[route]
  658. )
  659. console.warn(
  660. f"Page {route} is being redefined with the same component."
  661. )
  662. else:
  663. route_name = (
  664. f"`{route}` or `/`"
  665. if route == constants.PageNames.INDEX_ROUTE
  666. else f"`{route}`"
  667. )
  668. existing_component = self._unevaluated_pages[route].component
  669. raise exceptions.RouteValueError(
  670. f"Tried to add page {readable_name_from_component(component)} with route {route_name} but "
  671. f"page {readable_name_from_component(existing_component)} with the same route already exists. "
  672. "Make sure you do not have two pages with the same route."
  673. )
  674. # Setup dynamic args for the route.
  675. # this state assignment is only required for tests using the deprecated state kwarg for App
  676. state = self._state if self._state else State
  677. state.setup_dynamic_args(get_route_args(route))
  678. if on_load:
  679. self._load_events[route] = (
  680. on_load if isinstance(on_load, list) else [on_load]
  681. )
  682. self._unevaluated_pages[route] = unevaluated_page
  683. def _compile_page(self, route: str, save_page: bool = True):
  684. """Compile a page.
  685. Args:
  686. route: The route of the page to compile.
  687. save_page: If True, the compiled page is saved to self._pages.
  688. """
  689. n_states_before = len(all_base_state_classes)
  690. component, enable_state = compiler.compile_unevaluated_page(
  691. route, self._unevaluated_pages[route], self._state, self.style, self.theme
  692. )
  693. # Indicate that the app should use state.
  694. if enable_state:
  695. self._enable_state()
  696. # Indicate that evaluating this page creates one or more state classes.
  697. if len(all_base_state_classes) > n_states_before:
  698. self._stateful_pages[route] = None
  699. # Add the page.
  700. self._check_routes_conflict(route)
  701. if save_page:
  702. self._pages[route] = component
  703. def get_load_events(self, route: str) -> list[IndividualEventType[()]]:
  704. """Get the load events for a route.
  705. Args:
  706. route: The route to get the load events for.
  707. Returns:
  708. The load events for the route.
  709. """
  710. route = route.lstrip("/")
  711. if route == "":
  712. route = constants.PageNames.INDEX_ROUTE
  713. return self._load_events.get(route, [])
  714. def _check_routes_conflict(self, new_route: str):
  715. """Verify if there is any conflict between the new route and any existing route.
  716. Based on conflicts that NextJS would throw if not intercepted.
  717. Raises:
  718. RouteValueError: exception showing which conflict exist with the route to be added
  719. Args:
  720. new_route: the route being newly added.
  721. """
  722. from reflex.utils.exceptions import RouteValueError
  723. if "[" not in new_route:
  724. return
  725. segments = (
  726. constants.RouteRegex.SINGLE_SEGMENT,
  727. constants.RouteRegex.DOUBLE_SEGMENT,
  728. constants.RouteRegex.SINGLE_CATCHALL_SEGMENT,
  729. constants.RouteRegex.DOUBLE_CATCHALL_SEGMENT,
  730. )
  731. for route in self._pages:
  732. replaced_route = replace_brackets_with_keywords(route)
  733. for rw, r, nr in zip(
  734. replaced_route.split("/"),
  735. route.split("/"),
  736. new_route.split("/"),
  737. strict=False,
  738. ):
  739. if rw in segments and r != nr:
  740. # If the slugs in the segments of both routes are not the same, then the route is invalid
  741. raise RouteValueError(
  742. f"You cannot use different slug names for the same dynamic path in {route} and {new_route} ('{r}' != '{nr}')"
  743. )
  744. elif rw not in segments and r != nr:
  745. # if the section being compared in both routes is not a dynamic segment(i.e not wrapped in brackets)
  746. # then we are guaranteed that the route is valid and there's no need checking the rest.
  747. # eg. /posts/[id]/info/[slug1] and /posts/[id]/info1/[slug1] is always going to be valid since
  748. # info1 will break away into its own tree.
  749. break
  750. def add_custom_404_page(
  751. self,
  752. component: Component | ComponentCallable | None = None,
  753. title: str = constants.Page404.TITLE,
  754. image: str = constants.Page404.IMAGE,
  755. description: str = constants.Page404.DESCRIPTION,
  756. on_load: EventType[()] | None = None,
  757. meta: list[dict[str, str]] = constants.DefaultPage.META_LIST,
  758. ):
  759. """Define a custom 404 page for any url having no match.
  760. If there is no page defined on 'index' route, add the 404 page to it.
  761. If there is no global catchall defined, add the 404 page with a catchall.
  762. Args:
  763. component: The component to display at the page.
  764. title: The title of the page.
  765. image: The image to display on the page.
  766. description: The description of the page.
  767. on_load: The event handler(s) that will be called each time the page load.
  768. meta: The metadata of the page.
  769. """
  770. console.deprecate(
  771. feature_name="App.add_custom_404_page",
  772. reason=f"Use app.add_page(component, route='/{constants.Page404.SLUG}') instead.",
  773. deprecation_version="0.6.7",
  774. removal_version="0.8.0",
  775. )
  776. self.add_page(
  777. component=component,
  778. route=constants.Page404.SLUG,
  779. title=title or constants.Page404.TITLE,
  780. image=image or constants.Page404.IMAGE,
  781. description=description or constants.Page404.DESCRIPTION,
  782. on_load=on_load,
  783. meta=meta,
  784. )
  785. def _setup_admin_dash(self):
  786. """Setup the admin dash."""
  787. try:
  788. from starlette_admin.contrib.sqla.admin import Admin
  789. from starlette_admin.contrib.sqla.view import ModelView
  790. except ImportError:
  791. return
  792. # Get the admin dash.
  793. if not self._api:
  794. return
  795. admin_dash = self.admin_dash
  796. if admin_dash and admin_dash.models:
  797. # Build the admin dashboard
  798. admin = (
  799. admin_dash.admin
  800. if admin_dash.admin
  801. else Admin(
  802. engine=Model.get_db_engine(),
  803. title="Reflex Admin Dashboard",
  804. logo_url="https://reflex.dev/Reflex.svg",
  805. )
  806. )
  807. for model in admin_dash.models:
  808. view = admin_dash.view_overrides.get(model, ModelView)
  809. admin.add_view(view(model))
  810. admin.mount_to(self._api)
  811. def _get_frontend_packages(self, imports: dict[str, set[ImportVar]]):
  812. """Gets the frontend packages to be installed and filters out the unnecessary ones.
  813. Args:
  814. imports: A dictionary containing the imports used in the current page.
  815. Example:
  816. >>> _get_frontend_packages({"react": "16.14.0", "react-dom": "16.14.0"})
  817. """
  818. page_imports = {
  819. i
  820. for i, tags in imports.items()
  821. if i not in constants.PackageJson.DEPENDENCIES
  822. and i not in constants.PackageJson.DEV_DEPENDENCIES
  823. and not any(i.startswith(prefix) for prefix in ["/", "$/", ".", "next/"])
  824. and i != ""
  825. and any(tag.install for tag in tags)
  826. }
  827. pinned = {i.rpartition("@")[0] for i in page_imports if "@" in i}
  828. page_imports = {i for i in page_imports if i not in pinned}
  829. frontend_packages = get_config().frontend_packages
  830. _frontend_packages = []
  831. for package in frontend_packages:
  832. if package in (get_config().tailwind or {}).get("plugins", []):
  833. console.warn(
  834. f"Tailwind packages are inferred from 'plugins', remove `{package}` from `frontend_packages`"
  835. )
  836. continue
  837. if package in page_imports:
  838. console.warn(
  839. f"React packages and their dependencies are inferred from Component.library and Component.lib_dependencies, remove `{package}` from `frontend_packages`"
  840. )
  841. continue
  842. _frontend_packages.append(package)
  843. page_imports.update(_frontend_packages)
  844. prerequisites.install_frontend_packages(page_imports, get_config())
  845. def _app_root(self, app_wrappers: dict[tuple[int, str], Component]) -> Component:
  846. for component in tuple(app_wrappers.values()):
  847. app_wrappers.update(component._get_all_app_wrap_components())
  848. order = sorted(app_wrappers, key=lambda k: k[0], reverse=True)
  849. root = parent = copy.deepcopy(app_wrappers[order[0]])
  850. for key in order[1:]:
  851. child = copy.deepcopy(app_wrappers[key])
  852. parent.children.append(child)
  853. parent = child
  854. return root
  855. def _should_compile(self) -> bool:
  856. """Check if the app should be compiled.
  857. Returns:
  858. Whether the app should be compiled.
  859. """
  860. # Check the environment variable.
  861. if environment.REFLEX_SKIP_COMPILE.get():
  862. return False
  863. nocompile = prerequisites.get_web_dir() / constants.NOCOMPILE_FILE
  864. # Check the nocompile file.
  865. if nocompile.exists():
  866. # Delete the nocompile file
  867. nocompile.unlink(missing_ok=True)
  868. return False
  869. # By default, compile the app.
  870. return True
  871. def _add_overlay_to_component(self, component: Component) -> Component:
  872. if self.overlay_component is None:
  873. return component
  874. children = component.children
  875. overlay_component = self._generate_component(self.overlay_component)
  876. if children[0] == overlay_component:
  877. return component
  878. # recreate OverlayFragment with overlay_component as first child
  879. component = OverlayFragment.create(overlay_component, *children)
  880. return component
  881. def _setup_overlay_component(self):
  882. """If a State is not used and no overlay_component is specified, do not render the connection modal."""
  883. if self._state is None and self.overlay_component is default_overlay_component:
  884. self.overlay_component = None
  885. for k, component in self._pages.items():
  886. self._pages[k] = self._add_overlay_to_component(component)
  887. def _setup_sticky_badge(self):
  888. """Add the sticky badge to the app."""
  889. from reflex.components.component import memo
  890. @memo
  891. def memoized_badge():
  892. sticky_badge = sticky()
  893. sticky_badge._add_style_recursive({})
  894. return sticky_badge
  895. self.app_wraps[(0, "StickyBadge")] = lambda _: memoized_badge()
  896. def _apply_decorated_pages(self):
  897. """Add @rx.page decorated pages to the app.
  898. This has to be done in the MainThread for py38 and py39 compatibility, so the
  899. decorated pages are added to the app before the app is compiled (in a thread)
  900. to workaround REF-2172.
  901. This can move back into `compile_` when py39 support is dropped.
  902. """
  903. app_name = get_config().app_name
  904. # Add the @rx.page decorated pages to collect on_load events.
  905. for render, kwargs in DECORATED_PAGES[app_name]:
  906. self.add_page(render, **kwargs)
  907. def _validate_var_dependencies(self, state: type[BaseState] | None = None) -> None:
  908. """Validate the dependencies of the vars in the app.
  909. Args:
  910. state: The state to validate the dependencies for.
  911. Raises:
  912. VarDependencyError: When a computed var has an invalid dependency.
  913. """
  914. if not self._state:
  915. return
  916. if not state:
  917. state = self._state
  918. for var in state.computed_vars.values():
  919. if not var._cache:
  920. continue
  921. deps = var._deps(objclass=state)
  922. for state_name, dep_set in deps.items():
  923. state_cls = (
  924. state.get_root_state().get_class_substate(state_name)
  925. if state_name != state.get_full_name()
  926. else state
  927. )
  928. for dep in dep_set:
  929. if dep not in state_cls.vars and dep not in state_cls.backend_vars:
  930. raise exceptions.VarDependencyError(
  931. f"ComputedVar {var._js_expr} on state {state.__name__} has an invalid dependency {state_name}.{dep}"
  932. )
  933. for substate in state.class_subclasses:
  934. self._validate_var_dependencies(substate)
  935. def _compile(self, export: bool = False):
  936. """Compile the app and output it to the pages folder.
  937. Args:
  938. export: Whether to compile the app for export.
  939. Raises:
  940. ReflexRuntimeError: When any page uses state, but no rx.State subclass is defined.
  941. """
  942. from reflex.utils.exceptions import ReflexRuntimeError
  943. self._pages = {}
  944. def get_compilation_time() -> str:
  945. return str(datetime.now().time()).split(".")[0]
  946. should_compile = self._should_compile()
  947. backend_dir = prerequisites.get_backend_dir()
  948. if not should_compile and backend_dir.exists():
  949. stateful_pages_marker = backend_dir / constants.Dirs.STATEFUL_PAGES
  950. if stateful_pages_marker.exists():
  951. with stateful_pages_marker.open("r") as f:
  952. stateful_pages = json.load(f)
  953. for route in stateful_pages:
  954. console.debug(f"BE Evaluating stateful page: {route}")
  955. self._compile_page(route, save_page=False)
  956. self._enable_state()
  957. self._add_optional_endpoints()
  958. return
  959. # Render a default 404 page if the user didn't supply one
  960. if constants.Page404.SLUG not in self._unevaluated_pages:
  961. self.add_page(route=constants.Page404.SLUG)
  962. # Fix up the style.
  963. self.style = evaluate_style_namespaces(self.style)
  964. # Add the app wrappers.
  965. app_wrappers: dict[tuple[int, str], Component] = {
  966. # Default app wrap component renders {children}
  967. (0, "AppWrap"): AppWrap.create()
  968. }
  969. if self.theme is not None:
  970. # If a theme component was provided, wrap the app with it
  971. app_wrappers[(20, "Theme")] = self.theme
  972. # Get the env mode.
  973. config = get_config()
  974. if config.react_strict_mode:
  975. app_wrappers[(200, "StrictMode")] = StrictMode.create()
  976. if not should_compile:
  977. with console.timing("Evaluate Pages (Backend)"):
  978. for route in self._unevaluated_pages:
  979. console.debug(f"Evaluating page: {route}")
  980. self._compile_page(route, save_page=should_compile)
  981. # Save the pages which created new states at eval time.
  982. self._write_stateful_pages_marker()
  983. # Add the optional endpoints (_upload)
  984. self._add_optional_endpoints()
  985. return
  986. # Create a progress bar.
  987. progress = Progress(
  988. *Progress.get_default_columns()[:-1],
  989. MofNCompleteColumn(),
  990. TimeElapsedColumn(),
  991. )
  992. # try to be somewhat accurate - but still not 100%
  993. adhoc_steps_without_executor = 7
  994. fixed_pages_within_executor = 5
  995. progress.start()
  996. task = progress.add_task(
  997. f"[{get_compilation_time()}] Compiling:",
  998. total=len(self._pages)
  999. + (len(self._unevaluated_pages) * 2)
  1000. + fixed_pages_within_executor
  1001. + adhoc_steps_without_executor,
  1002. )
  1003. with console.timing("Evaluate Pages (Frontend)"):
  1004. performance_metrics: list[tuple[str, float]] = []
  1005. for route in self._unevaluated_pages:
  1006. console.debug(f"Evaluating page: {route}")
  1007. start = timer()
  1008. self._compile_page(route, save_page=should_compile)
  1009. end = timer()
  1010. performance_metrics.append((route, end - start))
  1011. progress.advance(task)
  1012. console.debug(
  1013. "Slowest pages:\n"
  1014. + "\n".join(
  1015. f"{route}: {time * 1000:.1f}ms"
  1016. for route, time in sorted(
  1017. performance_metrics, key=lambda x: x[1], reverse=True
  1018. )[:10]
  1019. )
  1020. )
  1021. # Save the pages which created new states at eval time.
  1022. self._write_stateful_pages_marker()
  1023. # Add the optional endpoints (_upload)
  1024. self._add_optional_endpoints()
  1025. self._validate_var_dependencies()
  1026. self._setup_overlay_component()
  1027. if config.show_built_with_reflex is None:
  1028. if (
  1029. get_compile_context() == constants.CompileContext.DEPLOY
  1030. and prerequisites.get_user_tier() in ["pro", "team", "enterprise"]
  1031. ):
  1032. config.show_built_with_reflex = False
  1033. else:
  1034. config.show_built_with_reflex = True
  1035. if is_prod_mode() and config.show_built_with_reflex:
  1036. self._setup_sticky_badge()
  1037. progress.advance(task)
  1038. # Store the compile results.
  1039. compile_results: list[tuple[str, str]] = []
  1040. progress.advance(task)
  1041. # Track imports found.
  1042. all_imports = {}
  1043. # This has to happen before compiling stateful components as that
  1044. # prevents recursive functions from reaching all components.
  1045. for component in self._pages.values():
  1046. # Add component._get_all_imports() to all_imports.
  1047. all_imports.update(component._get_all_imports())
  1048. # Add the app wrappers from this component.
  1049. app_wrappers.update(component._get_all_app_wrap_components())
  1050. if (toaster := self.toaster) is not None:
  1051. from reflex.components.component import memo
  1052. @memo
  1053. def memoized_toast_provider():
  1054. return toaster
  1055. toast_provider = Fragment.create(memoized_toast_provider())
  1056. app_wrappers[(1, "ToasterProvider")] = toast_provider
  1057. # Add the app wraps to the app.
  1058. for key, app_wrap in self.app_wraps.items():
  1059. component = app_wrap(self._state is not None)
  1060. if component is not None:
  1061. app_wrappers[key] = component
  1062. if self.error_boundary:
  1063. from reflex.compiler.compiler import into_component
  1064. console.deprecate(
  1065. feature_name="App.error_boundary",
  1066. reason="Use app_wraps instead.",
  1067. deprecation_version="0.7.1",
  1068. removal_version="0.8.0",
  1069. )
  1070. app_wrappers[(55, "ErrorBoundary")] = into_component(self.error_boundary)
  1071. # Perform auto-memoization of stateful components.
  1072. with console.timing("Auto-memoize StatefulComponents"):
  1073. (
  1074. stateful_components_path,
  1075. stateful_components_code,
  1076. page_components,
  1077. ) = compiler.compile_stateful_components(self._pages.values())
  1078. progress.advance(task)
  1079. # Catch "static" apps (that do not define a rx.State subclass) which are trying to access rx.State.
  1080. if code_uses_state_contexts(stateful_components_code) and self._state is None:
  1081. raise ReflexRuntimeError(
  1082. "To access rx.State in frontend components, at least one "
  1083. "subclass of rx.State must be defined in the app."
  1084. )
  1085. compile_results.append((stateful_components_path, stateful_components_code))
  1086. progress.advance(task)
  1087. # Compile the root document before fork.
  1088. compile_results.append(
  1089. compiler.compile_document_root(
  1090. self.head_components,
  1091. html_lang=self.html_lang,
  1092. html_custom_attrs=(
  1093. {**self.html_custom_attrs} if self.html_custom_attrs else {}
  1094. ),
  1095. )
  1096. )
  1097. progress.advance(task)
  1098. # Copy the assets.
  1099. assets_src = Path.cwd() / constants.Dirs.APP_ASSETS
  1100. if assets_src.is_dir():
  1101. with console.timing("Copy assets"):
  1102. path_ops.update_directory_tree(
  1103. src=assets_src,
  1104. dest=(
  1105. Path.cwd() / prerequisites.get_web_dir() / constants.Dirs.PUBLIC
  1106. ),
  1107. )
  1108. executor = ExecutorType.get_executor_from_environment()
  1109. for route, component in zip(self._pages, page_components, strict=True):
  1110. ExecutorSafeFunctions.COMPONENTS[route] = component
  1111. ExecutorSafeFunctions.STATE = self._state
  1112. with console.timing("Compile to Javascript"), executor as executor:
  1113. result_futures: list[concurrent.futures.Future[tuple[str, str]]] = []
  1114. def _submit_work(fn: Callable[..., tuple[str, str]], *args, **kwargs):
  1115. f = executor.submit(fn, *args, **kwargs)
  1116. f.add_done_callback(lambda _: progress.advance(task))
  1117. result_futures.append(f)
  1118. # Compile the pre-compiled pages.
  1119. for route in self._pages:
  1120. _submit_work(
  1121. ExecutorSafeFunctions.compile_page,
  1122. route,
  1123. )
  1124. # Compile the root stylesheet with base styles.
  1125. _submit_work(compiler.compile_root_stylesheet, self.stylesheets)
  1126. # Compile the theme.
  1127. _submit_work(compile_theme, self.style)
  1128. # Compile the Tailwind config.
  1129. if config.tailwind is not None:
  1130. config.tailwind["content"] = config.tailwind.get(
  1131. "content", constants.Tailwind.CONTENT
  1132. )
  1133. _submit_work(compiler.compile_tailwind, config.tailwind)
  1134. else:
  1135. _submit_work(compiler.remove_tailwind_from_postcss)
  1136. # Wait for all compilation tasks to complete.
  1137. compile_results.extend(
  1138. future.result()
  1139. for future in concurrent.futures.as_completed(result_futures)
  1140. )
  1141. app_root = self._app_root(app_wrappers=app_wrappers)
  1142. # Get imports from AppWrap components.
  1143. all_imports.update(app_root._get_all_imports())
  1144. progress.advance(task)
  1145. # Compile the contexts.
  1146. compile_results.append(
  1147. compiler.compile_contexts(self._state, self.theme),
  1148. )
  1149. if self.theme is not None:
  1150. # Fix #2992 by removing the top-level appearance prop
  1151. self.theme.appearance = None
  1152. progress.advance(task)
  1153. # Compile the app root.
  1154. compile_results.append(
  1155. compiler.compile_app(app_root),
  1156. )
  1157. progress.advance(task)
  1158. # Compile custom components.
  1159. (
  1160. custom_components_output,
  1161. custom_components_result,
  1162. custom_components_imports,
  1163. ) = compiler.compile_components(set(CUSTOM_COMPONENTS.values()))
  1164. compile_results.append((custom_components_output, custom_components_result))
  1165. all_imports.update(custom_components_imports)
  1166. progress.advance(task)
  1167. progress.stop()
  1168. # Install frontend packages.
  1169. with console.timing("Install Frontend Packages"):
  1170. self._get_frontend_packages(all_imports)
  1171. # Setup the next.config.js
  1172. transpile_packages = [
  1173. package
  1174. for package, import_vars in all_imports.items()
  1175. if any(import_var.transpile for import_var in import_vars)
  1176. ]
  1177. prerequisites.update_next_config(
  1178. export=export,
  1179. transpile_packages=transpile_packages,
  1180. )
  1181. if is_prod_mode():
  1182. # Empty the .web pages directory.
  1183. compiler.purge_web_pages_dir()
  1184. else:
  1185. # In dev mode, delete removed pages and update existing pages.
  1186. keep_files = [Path(output_path) for output_path, _ in compile_results]
  1187. for p in Path(prerequisites.get_web_dir() / constants.Dirs.PAGES).rglob(
  1188. "*"
  1189. ):
  1190. if p.is_file() and p not in keep_files:
  1191. # Remove pages that are no longer in the app.
  1192. p.unlink()
  1193. with console.timing("Write to Disk"):
  1194. for output_path, code in compile_results:
  1195. compiler_utils.write_page(output_path, code)
  1196. def _write_stateful_pages_marker(self):
  1197. """Write list of routes that create dynamic states for the backend to use later."""
  1198. if self._state is not None:
  1199. stateful_pages_marker = (
  1200. prerequisites.get_backend_dir() / constants.Dirs.STATEFUL_PAGES
  1201. )
  1202. stateful_pages_marker.parent.mkdir(parents=True, exist_ok=True)
  1203. with stateful_pages_marker.open("w") as f:
  1204. json.dump(list(self._stateful_pages), f)
  1205. def add_all_routes_endpoint(self):
  1206. """Add an endpoint to the app that returns all the routes."""
  1207. if not self._api:
  1208. return
  1209. async def all_routes(_request: Request) -> Response:
  1210. return JSONResponse(list(self._unevaluated_pages.keys()))
  1211. self._api.add_route(
  1212. str(constants.Endpoint.ALL_ROUTES), all_routes, methods=["GET"]
  1213. )
  1214. @contextlib.asynccontextmanager
  1215. async def modify_state(self, token: str) -> AsyncIterator[BaseState]:
  1216. """Modify the state out of band.
  1217. Args:
  1218. token: The token to modify the state for.
  1219. Yields:
  1220. The state to modify.
  1221. Raises:
  1222. RuntimeError: If the app has not been initialized yet.
  1223. """
  1224. if self.event_namespace is None:
  1225. raise RuntimeError("App has not been initialized yet.")
  1226. # Get exclusive access to the state.
  1227. async with self.state_manager.modify_state(token) as state:
  1228. # No other event handler can modify the state while in this context.
  1229. yield state
  1230. delta = await state._get_resolved_delta()
  1231. if delta:
  1232. # When the state is modified reset dirty status and emit the delta to the frontend.
  1233. state._clean()
  1234. await self.event_namespace.emit_update(
  1235. update=StateUpdate(delta=delta),
  1236. sid=state.router.session.session_id,
  1237. )
  1238. def _process_background(
  1239. self, state: BaseState, event: Event
  1240. ) -> asyncio.Task | None:
  1241. """Process an event in the background and emit updates as they arrive.
  1242. Args:
  1243. state: The state to process the event for.
  1244. event: The event to process.
  1245. Returns:
  1246. Task if the event was backgroundable, otherwise None
  1247. """
  1248. substate, handler = state._get_event_handler(event)
  1249. if not handler.is_background:
  1250. return None
  1251. async def _coro():
  1252. """Coroutine to process the event and emit updates inside an asyncio.Task.
  1253. Raises:
  1254. RuntimeError: If the app has not been initialized yet.
  1255. """
  1256. if self.event_namespace is None:
  1257. raise RuntimeError("App has not been initialized yet.")
  1258. # Process the event.
  1259. async for update in state._process_event(
  1260. handler=handler, state=substate, payload=event.payload
  1261. ):
  1262. # Postprocess the event.
  1263. update = await self._postprocess(state, event, update)
  1264. # Send the update to the client.
  1265. await self.event_namespace.emit_update(
  1266. update=update,
  1267. sid=state.router.session.session_id,
  1268. )
  1269. task = asyncio.create_task(_coro())
  1270. self._background_tasks.add(task)
  1271. # Clean up task from background_tasks set when complete.
  1272. task.add_done_callback(self._background_tasks.discard)
  1273. return task
  1274. def _validate_exception_handlers(self):
  1275. """Validate the custom event exception handlers for front- and backend.
  1276. Raises:
  1277. ValueError: If the custom exception handlers are invalid.
  1278. """
  1279. frontend_arg_spec = {
  1280. "exception": Exception,
  1281. }
  1282. backend_arg_spec = {
  1283. "exception": Exception,
  1284. }
  1285. for handler_domain, handler_fn, handler_spec in zip(
  1286. ["frontend", "backend"],
  1287. [self.frontend_exception_handler, self.backend_exception_handler],
  1288. [
  1289. frontend_arg_spec,
  1290. backend_arg_spec,
  1291. ],
  1292. strict=True,
  1293. ):
  1294. if hasattr(handler_fn, "__name__"):
  1295. _fn_name = handler_fn.__name__
  1296. else:
  1297. _fn_name = type(handler_fn).__name__
  1298. if isinstance(handler_fn, functools.partial):
  1299. raise ValueError(
  1300. f"Provided custom {handler_domain} exception handler `{_fn_name}` is a partial function. Please provide a named function instead."
  1301. )
  1302. if not callable(handler_fn):
  1303. raise ValueError(
  1304. f"Provided custom {handler_domain} exception handler `{_fn_name}` is not a function."
  1305. )
  1306. # Allow named functions only as lambda functions cannot be introspected
  1307. if _fn_name == "<lambda>":
  1308. raise ValueError(
  1309. f"Provided custom {handler_domain} exception handler `{_fn_name}` is a lambda function. Please use a named function instead."
  1310. )
  1311. # Check if the function has the necessary annotations and types in the right order
  1312. argspec = inspect.getfullargspec(handler_fn)
  1313. arg_annotations = {
  1314. k: eval(v) if isinstance(v, str) else v
  1315. for k, v in argspec.annotations.items()
  1316. if k not in ["args", "kwargs", "return"]
  1317. }
  1318. for required_arg_index, required_arg in enumerate(handler_spec):
  1319. if required_arg not in arg_annotations:
  1320. raise ValueError(
  1321. f"Provided custom {handler_domain} exception handler `{_fn_name}` does not take the required argument `{required_arg}`"
  1322. )
  1323. elif (
  1324. not list(arg_annotations.keys())[required_arg_index] == required_arg
  1325. ):
  1326. raise ValueError(
  1327. f"Provided custom {handler_domain} exception handler `{_fn_name}` has the wrong argument order."
  1328. f"Expected `{required_arg}` as the {required_arg_index + 1} argument but got `{list(arg_annotations.keys())[required_arg_index]}`"
  1329. )
  1330. if not issubclass(arg_annotations[required_arg], Exception):
  1331. raise ValueError(
  1332. f"Provided custom {handler_domain} exception handler `{_fn_name}` has the wrong type for {required_arg} argument."
  1333. f"Expected to be `Exception` but got `{arg_annotations[required_arg]}`"
  1334. )
  1335. # Check if the return type is valid for backend exception handler
  1336. if handler_domain == "backend":
  1337. sig = inspect.signature(self.backend_exception_handler)
  1338. return_type = (
  1339. eval(sig.return_annotation)
  1340. if isinstance(sig.return_annotation, str)
  1341. else sig.return_annotation
  1342. )
  1343. valid = bool(
  1344. return_type == EventSpec
  1345. or return_type == EventSpec | None
  1346. or return_type == list[EventSpec]
  1347. or return_type == inspect.Signature.empty
  1348. or return_type is None
  1349. )
  1350. if not valid:
  1351. raise ValueError(
  1352. f"Provided custom {handler_domain} exception handler `{_fn_name}` has the wrong return type."
  1353. f"Expected `EventSpec | list[EventSpec] | None` but got `{return_type}`"
  1354. )
  1355. async def process(
  1356. app: App, event: Event, sid: str, headers: dict, client_ip: str
  1357. ) -> AsyncIterator[StateUpdate]:
  1358. """Process an event.
  1359. Args:
  1360. app: The app to process the event for.
  1361. event: The event to process.
  1362. sid: The Socket.IO session id.
  1363. headers: The client headers.
  1364. client_ip: The client_ip.
  1365. Raises:
  1366. Exception: If a reflex specific error occurs during processing the event.
  1367. Yields:
  1368. The state updates after processing the event.
  1369. """
  1370. from reflex.utils import telemetry
  1371. try:
  1372. # Add request data to the state.
  1373. router_data = event.router_data
  1374. router_data.update(
  1375. {
  1376. constants.RouteVar.QUERY: format.format_query_params(event.router_data),
  1377. constants.RouteVar.CLIENT_TOKEN: event.token,
  1378. constants.RouteVar.SESSION_ID: sid,
  1379. constants.RouteVar.HEADERS: headers,
  1380. constants.RouteVar.CLIENT_IP: client_ip,
  1381. }
  1382. )
  1383. # Get the state for the session exclusively.
  1384. async with app.state_manager.modify_state(event.substate_token) as state:
  1385. # When this is a brand new instance of the state, signal the
  1386. # frontend to reload before processing it.
  1387. if (
  1388. not state.router_data
  1389. and event.name != get_hydrate_event(state)
  1390. and app.event_namespace is not None
  1391. ):
  1392. await asyncio.create_task(
  1393. app.event_namespace.emit(
  1394. "reload",
  1395. data=event,
  1396. to=sid,
  1397. )
  1398. )
  1399. return
  1400. # re-assign only when the value is different
  1401. if state.router_data != router_data:
  1402. # assignment will recurse into substates and force recalculation of
  1403. # dependent ComputedVar (dynamic route variables)
  1404. state.router_data = router_data
  1405. state.router = RouterData(router_data)
  1406. # Preprocess the event.
  1407. update = await app._preprocess(state, event)
  1408. # If there was an update, yield it.
  1409. if update is not None:
  1410. yield update
  1411. # Only process the event if there is no update.
  1412. else:
  1413. if app._process_background(state, event) is not None:
  1414. # `final=True` allows the frontend send more events immediately.
  1415. yield StateUpdate(final=True)
  1416. else:
  1417. # Process the event synchronously.
  1418. async for update in state._process(event):
  1419. # Postprocess the event.
  1420. update = await app._postprocess(state, event, update)
  1421. # Yield the update.
  1422. yield update
  1423. except Exception as ex:
  1424. telemetry.send_error(ex, context="backend")
  1425. app.backend_exception_handler(ex)
  1426. raise
  1427. async def ping(_request: Request) -> Response:
  1428. """Test API endpoint.
  1429. Args:
  1430. _request: The Starlette request object.
  1431. Returns:
  1432. The response.
  1433. """
  1434. return JSONResponse("pong")
  1435. async def health(_request: Request) -> JSONResponse:
  1436. """Health check endpoint to assess the status of the database and Redis services.
  1437. Args:
  1438. _request: The Starlette request object.
  1439. Returns:
  1440. JSONResponse: A JSON object with the health status:
  1441. - "status" (bool): Overall health, True if all checks pass.
  1442. - "db" (bool or str): Database status - True, False, or "NA".
  1443. - "redis" (bool or str): Redis status - True, False, or "NA".
  1444. """
  1445. health_status = {"status": True}
  1446. status_code = 200
  1447. tasks = []
  1448. if prerequisites.check_db_used():
  1449. tasks.append(get_db_status())
  1450. if prerequisites.check_redis_used():
  1451. tasks.append(prerequisites.get_redis_status())
  1452. results = await asyncio.gather(*tasks)
  1453. for result in results:
  1454. health_status |= result
  1455. if "redis" in health_status and health_status["redis"] is None:
  1456. health_status["redis"] = False
  1457. if not all(health_status.values()):
  1458. health_status["status"] = False
  1459. status_code = 503
  1460. return JSONResponse(content=health_status, status_code=status_code)
  1461. def upload(app: App):
  1462. """Upload a file.
  1463. Args:
  1464. app: The app to upload the file for.
  1465. Returns:
  1466. The upload function.
  1467. """
  1468. async def upload_file(request: Request):
  1469. """Upload a file.
  1470. Args:
  1471. request: The Starlette request object.
  1472. Returns:
  1473. StreamingResponse yielding newline-delimited JSON of StateUpdate
  1474. emitted by the upload handler.
  1475. Raises:
  1476. UploadValueError: if there are no args with supported annotation.
  1477. UploadTypeError: if a background task is used as the handler.
  1478. HTTPException: when the request does not include token / handler headers.
  1479. """
  1480. from reflex.utils.exceptions import UploadTypeError, UploadValueError
  1481. # Get the files from the request.
  1482. try:
  1483. files = await request.form()
  1484. except ClientDisconnect:
  1485. return Response() # user cancelled
  1486. files = files.getlist("files")
  1487. if not files:
  1488. raise UploadValueError("No files were uploaded.")
  1489. token = request.headers.get("reflex-client-token")
  1490. handler = request.headers.get("reflex-event-handler")
  1491. if not token or not handler:
  1492. raise HTTPException(
  1493. status_code=400,
  1494. detail="Missing reflex-client-token or reflex-event-handler header.",
  1495. )
  1496. # Get the state for the session.
  1497. substate_token = _substate_key(token, handler.rpartition(".")[0])
  1498. state = await app.state_manager.get_state(substate_token)
  1499. # get the current session ID
  1500. # get the current state(parent state/substate)
  1501. path = handler.split(".")[:-1]
  1502. current_state = state.get_substate(path)
  1503. handler_upload_param = ()
  1504. # get handler function
  1505. func = getattr(type(current_state), handler.split(".")[-1])
  1506. # check if there exists any handler args with annotation, list[UploadFile]
  1507. if isinstance(func, EventHandler):
  1508. if func.is_background:
  1509. raise UploadTypeError(
  1510. f"@rx.event(background=True) is not supported for upload handler `{handler}`.",
  1511. )
  1512. func = func.fn
  1513. if isinstance(func, functools.partial):
  1514. func = func.func
  1515. for k, v in get_type_hints(func).items():
  1516. if types.is_generic_alias(v) and types._issubclass(
  1517. get_args(v)[0],
  1518. UploadFile,
  1519. ):
  1520. handler_upload_param = (k, v)
  1521. break
  1522. if not handler_upload_param:
  1523. raise UploadValueError(
  1524. f"`{handler}` handler should have a parameter annotated as "
  1525. "list[rx.UploadFile]"
  1526. )
  1527. # Make a copy of the files as they are closed after the request.
  1528. # This behaviour changed from fastapi 0.103.0 to 0.103.1 as the
  1529. # AsyncExitStack was removed from the request scope and is now
  1530. # part of the routing function which closes this before the
  1531. # event is handled.
  1532. file_copies = []
  1533. for file in files:
  1534. if not isinstance(file, StarletteUploadFile):
  1535. raise UploadValueError(
  1536. "Uploaded file is not an UploadFile." + str(file)
  1537. )
  1538. content_copy = io.BytesIO()
  1539. content_copy.write(await file.read())
  1540. content_copy.seek(0)
  1541. file_copies.append(
  1542. UploadFile(
  1543. file=content_copy,
  1544. path=Path(file.filename.lstrip("/")) if file.filename else None,
  1545. _deprecated_filename=file.filename,
  1546. size=file.size,
  1547. headers=file.headers,
  1548. )
  1549. )
  1550. event = Event(
  1551. token=token,
  1552. name=handler,
  1553. payload={handler_upload_param[0]: file_copies},
  1554. )
  1555. async def _ndjson_updates():
  1556. """Process the upload event, generating ndjson updates.
  1557. Yields:
  1558. Each state update as JSON followed by a new line.
  1559. """
  1560. # Process the event.
  1561. async with app.state_manager.modify_state(event.substate_token) as state:
  1562. async for update in state._process(event):
  1563. # Postprocess the event.
  1564. update = await app._postprocess(state, event, update)
  1565. yield update.json() + "\n"
  1566. # Stream updates to client
  1567. return StreamingResponse(
  1568. _ndjson_updates(),
  1569. media_type="application/x-ndjson",
  1570. )
  1571. return upload_file
  1572. class EventNamespace(AsyncNamespace):
  1573. """The event namespace."""
  1574. # The application object.
  1575. app: App
  1576. # Keep a mapping between socket ID and client token.
  1577. token_to_sid: dict[str, str]
  1578. # Keep a mapping between client token and socket ID.
  1579. sid_to_token: dict[str, str]
  1580. def __init__(self, namespace: str, app: App):
  1581. """Initialize the event namespace.
  1582. Args:
  1583. namespace: The namespace.
  1584. app: The application object.
  1585. """
  1586. super().__init__(namespace)
  1587. self.token_to_sid = {}
  1588. self.sid_to_token = {}
  1589. self.app = app
  1590. def on_connect(self, sid: str, environ: dict):
  1591. """Event for when the websocket is connected.
  1592. Args:
  1593. sid: The Socket.IO session id.
  1594. environ: The request information, including HTTP headers.
  1595. """
  1596. subprotocol = environ.get("HTTP_SEC_WEBSOCKET_PROTOCOL")
  1597. if subprotocol and subprotocol != constants.Reflex.VERSION:
  1598. console.warn(
  1599. f"Frontend version {subprotocol} for session {sid} does not match the backend version {constants.Reflex.VERSION}."
  1600. )
  1601. def on_disconnect(self, sid: str):
  1602. """Event for when the websocket disconnects.
  1603. Args:
  1604. sid: The Socket.IO session id.
  1605. """
  1606. disconnect_token = self.sid_to_token.pop(sid, None)
  1607. if disconnect_token:
  1608. self.token_to_sid.pop(disconnect_token, None)
  1609. async def emit_update(self, update: StateUpdate, sid: str) -> None:
  1610. """Emit an update to the client.
  1611. Args:
  1612. update: The state update to send.
  1613. sid: The Socket.IO session id.
  1614. """
  1615. # Creating a task prevents the update from being blocked behind other coroutines.
  1616. await asyncio.create_task(
  1617. self.emit(str(constants.SocketEvent.EVENT), update, to=sid)
  1618. )
  1619. async def on_event(self, sid: str, data: Any):
  1620. """Event for receiving front-end websocket events.
  1621. Raises:
  1622. RuntimeError: If the Socket.IO is badly initialized.
  1623. Args:
  1624. sid: The Socket.IO session id.
  1625. data: The event data.
  1626. Raises:
  1627. EventDeserializationError: If the event data is not a dictionary.
  1628. """
  1629. fields = data
  1630. if isinstance(fields, str):
  1631. console.warn(
  1632. "Received event data as a string. This generally should not happen and may indicate a bug."
  1633. f" Event data: {fields}"
  1634. )
  1635. try:
  1636. fields = json.loads(fields)
  1637. except json.JSONDecodeError as ex:
  1638. raise exceptions.EventDeserializationError(
  1639. f"Failed to deserialize event data: {fields}."
  1640. ) from ex
  1641. if not isinstance(fields, dict):
  1642. raise exceptions.EventDeserializationError(
  1643. f"Event data must be a dictionary, but received {fields} of type {type(fields)}."
  1644. )
  1645. try:
  1646. # Get the event.
  1647. event = Event(**{k: v for k, v in fields.items() if k in _EVENT_FIELDS})
  1648. except (TypeError, ValueError) as ex:
  1649. raise exceptions.EventDeserializationError(
  1650. f"Failed to deserialize event data: {fields}."
  1651. ) from ex
  1652. self.token_to_sid[event.token] = sid
  1653. self.sid_to_token[sid] = event.token
  1654. # Get the event environment.
  1655. if self.app.sio is None:
  1656. raise RuntimeError("Socket.IO is not initialized.")
  1657. environ = self.app.sio.get_environ(sid, self.namespace)
  1658. if environ is None:
  1659. raise RuntimeError("Socket.IO environ is not initialized.")
  1660. # Get the client headers.
  1661. headers = {
  1662. k.decode("utf-8"): v.decode("utf-8")
  1663. for (k, v) in environ["asgi.scope"]["headers"]
  1664. }
  1665. # Get the client IP
  1666. try:
  1667. client_ip = environ["asgi.scope"]["client"][0]
  1668. except (KeyError, IndexError):
  1669. client_ip = environ.get("REMOTE_ADDR", "0.0.0.0")
  1670. # Process the events.
  1671. async for update in process(self.app, event, sid, headers, client_ip):
  1672. # Emit the update from processing the event.
  1673. await self.emit_update(update=update, sid=sid)
  1674. async def on_ping(self, sid: str):
  1675. """Event for testing the API endpoint.
  1676. Args:
  1677. sid: The Socket.IO session id.
  1678. """
  1679. # Emit the test event.
  1680. await self.emit(str(constants.SocketEvent.PING), "pong", to=sid)