app.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. """The main Reflex app."""
  2. import asyncio
  3. import inspect
  4. import os
  5. from multiprocessing.pool import ThreadPool
  6. from typing import (
  7. Any,
  8. AsyncIterator,
  9. Callable,
  10. Coroutine,
  11. Dict,
  12. List,
  13. Optional,
  14. Tuple,
  15. Type,
  16. Union,
  17. )
  18. from fastapi import FastAPI, UploadFile
  19. from fastapi.middleware import cors
  20. from rich.progress import MofNCompleteColumn, Progress, TimeElapsedColumn
  21. from socketio import ASGIApp, AsyncNamespace, AsyncServer
  22. from starlette_admin.contrib.sqla.admin import Admin
  23. from starlette_admin.contrib.sqla.view import ModelView
  24. from reflex import constants
  25. from reflex.admin import AdminDash
  26. from reflex.base import Base
  27. from reflex.compiler import compiler
  28. from reflex.compiler import utils as compiler_utils
  29. from reflex.components.component import Component, ComponentStyle
  30. from reflex.components.layout.fragment import Fragment
  31. from reflex.config import get_config
  32. from reflex.event import Event, EventHandler, EventSpec
  33. from reflex.middleware import HydrateMiddleware, Middleware
  34. from reflex.model import Model
  35. from reflex.route import (
  36. DECORATED_ROUTES,
  37. catchall_in_route,
  38. catchall_prefix,
  39. get_route_args,
  40. verify_route_validity,
  41. )
  42. from reflex.state import DefaultState, State, StateManager, StateUpdate
  43. from reflex.utils import console, format, types
  44. # Define custom types.
  45. ComponentCallable = Callable[[], Component]
  46. Reducer = Callable[[Event], Coroutine[Any, Any, StateUpdate]]
  47. class App(Base):
  48. """A Reflex application."""
  49. # A map from a page route to the component to render.
  50. pages: Dict[str, Component] = {}
  51. # A list of URLs to stylesheets to include in the app.
  52. stylesheets: List[str] = []
  53. # The backend API object.
  54. api: FastAPI = None # type: ignore
  55. # The Socket.IO AsyncServer.
  56. sio: Optional[AsyncServer] = None
  57. # The socket app.
  58. socket_app: Optional[ASGIApp] = None
  59. # The state class to use for the app.
  60. state: Type[State] = DefaultState
  61. # Class to manage many client states.
  62. state_manager: StateManager = StateManager()
  63. # The styling to apply to each component.
  64. style: ComponentStyle = {}
  65. # Middleware to add to the app.
  66. middleware: List[Middleware] = []
  67. # List of event handlers to trigger when a page loads.
  68. load_events: Dict[str, List[Union[EventHandler, EventSpec]]] = {}
  69. # Admin dashboard
  70. admin_dash: Optional[AdminDash] = None
  71. # The component to render if there is a connection error to the server.
  72. connect_error_component: Optional[Component] = None
  73. # The async server name space
  74. event_namespace: Optional[AsyncNamespace] = None
  75. def __init__(self, *args, **kwargs):
  76. """Initialize the app.
  77. Args:
  78. *args: Args to initialize the app with.
  79. **kwargs: Kwargs to initialize the app with.
  80. Raises:
  81. ValueError: If the event namespace is not provided in the config.
  82. Also, if there are multiple client subclasses of rx.State(Subclasses of rx.State should consist
  83. of the DefaultState and the client app state).
  84. """
  85. super().__init__(*args, **kwargs)
  86. state_subclasses = State.__subclasses__()
  87. is_testing_env = constants.PYTEST_CURRENT_TEST in os.environ
  88. # Special case to allow test cases have multiple subclasses of rx.State.
  89. if not is_testing_env:
  90. # Only the default state and the client state should be allowed as subclasses.
  91. if len(state_subclasses) > 2:
  92. raise ValueError(
  93. "rx.State has been subclassed multiple times. Only one subclass is allowed"
  94. )
  95. if self.state != DefaultState:
  96. console.deprecate(
  97. "Passing the state as keyword argument to `rx.App` is deprecated. "
  98. "The base state will automatically be inferred as the subclass of `rx.State`."
  99. )
  100. self.state = state_subclasses[-1]
  101. # Get the config
  102. config = get_config()
  103. # Add middleware.
  104. self.middleware.append(HydrateMiddleware())
  105. # Set up the state manager.
  106. self.state_manager.setup(state=self.state)
  107. # Set up the API.
  108. self.api = FastAPI()
  109. self.add_cors()
  110. self.add_default_endpoints()
  111. # Set up the Socket.IO AsyncServer.
  112. self.sio = AsyncServer(
  113. async_mode="asgi",
  114. cors_allowed_origins="*"
  115. if config.cors_allowed_origins == constants.CORS_ALLOWED_ORIGINS
  116. else config.cors_allowed_origins,
  117. cors_credentials=config.cors_credentials,
  118. max_http_buffer_size=config.polling_max_http_buffer_size,
  119. ping_interval=constants.PING_INTERVAL,
  120. ping_timeout=constants.PING_TIMEOUT,
  121. )
  122. # Create the socket app. Note event endpoint constant replaces the default 'socket.io' path.
  123. self.socket_app = ASGIApp(self.sio, socketio_path="")
  124. namespace = config.get_event_namespace()
  125. if not namespace:
  126. raise ValueError("event namespace must be provided in the config.")
  127. # Create the event namespace and attach the main app. Not related to any paths.
  128. self.event_namespace = EventNamespace(namespace, self)
  129. # Register the event namespace with the socket.
  130. self.sio.register_namespace(self.event_namespace)
  131. # Mount the socket app with the API.
  132. self.api.mount(str(constants.Endpoint.EVENT), self.socket_app)
  133. # Set up the admin dash.
  134. self.setup_admin_dash()
  135. def __repr__(self) -> str:
  136. """Get the string representation of the app.
  137. Returns:
  138. The string representation of the app.
  139. """
  140. return f"<App state={self.state.__name__}>"
  141. def __call__(self) -> FastAPI:
  142. """Run the backend api instance.
  143. Returns:
  144. The backend api.
  145. """
  146. return self.api
  147. def add_default_endpoints(self):
  148. """Add the default endpoints."""
  149. # To test the server.
  150. self.api.get(str(constants.Endpoint.PING))(ping)
  151. # To upload files.
  152. self.api.post(str(constants.Endpoint.UPLOAD))(upload(self))
  153. def add_cors(self):
  154. """Add CORS middleware to the app."""
  155. self.api.add_middleware(
  156. cors.CORSMiddleware,
  157. allow_credentials=True,
  158. allow_methods=["*"],
  159. allow_headers=["*"],
  160. allow_origins=["*"],
  161. )
  162. async def preprocess(self, state: State, event: Event) -> Optional[StateUpdate]:
  163. """Preprocess the event.
  164. This is where middleware can modify the event before it is processed.
  165. Each middleware is called in the order it was added to the app.
  166. If a middleware returns an update, the event is not processed and the
  167. update is returned.
  168. Args:
  169. state: The state to preprocess.
  170. event: The event to preprocess.
  171. Returns:
  172. An optional state to return.
  173. """
  174. for middleware in self.middleware:
  175. if asyncio.iscoroutinefunction(middleware.preprocess):
  176. out = await middleware.preprocess(app=self, state=state, event=event)
  177. else:
  178. out = middleware.preprocess(app=self, state=state, event=event)
  179. if out is not None:
  180. return out # type: ignore
  181. async def postprocess(
  182. self, state: State, event: Event, update: StateUpdate
  183. ) -> StateUpdate:
  184. """Postprocess the event.
  185. This is where middleware can modify the delta after it is processed.
  186. Each middleware is called in the order it was added to the app.
  187. Args:
  188. state: The state to postprocess.
  189. event: The event to postprocess.
  190. update: The current state update.
  191. Returns:
  192. The state update to return.
  193. """
  194. for middleware in self.middleware:
  195. if asyncio.iscoroutinefunction(middleware.postprocess):
  196. out = await middleware.postprocess(
  197. app=self, state=state, event=event, update=update
  198. )
  199. else:
  200. out = middleware.postprocess(
  201. app=self, state=state, event=event, update=update
  202. )
  203. if out is not None:
  204. return out # type: ignore
  205. return update
  206. def add_middleware(self, middleware: Middleware, index: Optional[int] = None):
  207. """Add middleware to the app.
  208. Args:
  209. middleware: The middleware to add.
  210. index: The index to add the middleware at.
  211. """
  212. if index is None:
  213. self.middleware.append(middleware)
  214. else:
  215. self.middleware.insert(index, middleware)
  216. def add_page(
  217. self,
  218. component: Union[Component, ComponentCallable],
  219. route: Optional[str] = None,
  220. title: str = constants.DEFAULT_TITLE,
  221. description: str = constants.DEFAULT_DESCRIPTION,
  222. image=constants.DEFAULT_IMAGE,
  223. on_load: Optional[
  224. Union[EventHandler, EventSpec, List[Union[EventHandler, EventSpec]]]
  225. ] = None,
  226. meta: List[Dict] = constants.DEFAULT_META_LIST,
  227. script_tags: Optional[List[Component]] = None,
  228. ):
  229. """Add a page to the app.
  230. If the component is a callable, by default the route is the name of the
  231. function. Otherwise, a route must be provided.
  232. Args:
  233. component: The component to display at the page.
  234. route: The route to display the component at.
  235. title: The title of the page.
  236. description: The description of the page.
  237. image: The image to display on the page.
  238. on_load: The event handler(s) that will be called each time the page load.
  239. meta: The metadata of the page.
  240. script_tags: List of script tags to be added to component
  241. Raises:
  242. TypeError: If an invalid var operation is used.
  243. """
  244. # If the route is not set, get it from the callable.
  245. if route is None:
  246. assert isinstance(
  247. component, Callable
  248. ), "Route must be set if component is not a callable."
  249. route = component.__name__
  250. # Check if the route given is valid
  251. verify_route_validity(route)
  252. # Apply dynamic args to the route.
  253. self.state.setup_dynamic_args(get_route_args(route))
  254. # Generate the component if it is a callable.
  255. try:
  256. component = component if isinstance(component, Component) else component()
  257. except TypeError as e:
  258. message = str(e)
  259. if "BaseVar" in message or "ComputedVar" in message:
  260. raise TypeError(
  261. "You may be trying to use an invalid Python function on a state var. "
  262. "When referencing a var inside your render code, only limited var operations are supported. "
  263. "See the var operation docs here: https://pynecone.io/docs/state/vars "
  264. ) from e
  265. raise e
  266. # Wrap the component in a fragment.
  267. component = Fragment.create(component)
  268. # Add meta information to the component.
  269. compiler_utils.add_meta(
  270. component,
  271. title=title,
  272. image=image,
  273. description=description,
  274. meta=meta,
  275. )
  276. # Add script tags if given
  277. if script_tags:
  278. component.children.extend(script_tags)
  279. # Format the route.
  280. route = format.format_route(route)
  281. # Add the page.
  282. self._check_routes_conflict(route)
  283. self.pages[route] = component
  284. # Add the load events.
  285. if on_load:
  286. if not isinstance(on_load, list):
  287. on_load = [on_load]
  288. self.load_events[route] = on_load
  289. def get_load_events(self, route: str) -> List[Union[EventHandler, EventSpec]]:
  290. """Get the load events for a route.
  291. Args:
  292. route: The route to get the load events for.
  293. Returns:
  294. The load events for the route.
  295. """
  296. route = route.lstrip("/")
  297. if route == "":
  298. route = constants.INDEX_ROUTE
  299. return self.load_events.get(route, [])
  300. def _check_routes_conflict(self, new_route: str):
  301. """Verify if there is any conflict between the new route and any existing route.
  302. Based on conflicts that NextJS would throw if not intercepted.
  303. Raises:
  304. ValueError: exception showing which conflict exist with the route to be added
  305. Args:
  306. new_route: the route being newly added.
  307. """
  308. newroute_catchall = catchall_in_route(new_route)
  309. if not newroute_catchall:
  310. return
  311. for route in self.pages:
  312. route = "" if route == "index" else route
  313. if new_route.startswith(f"{route}/[[..."):
  314. raise ValueError(
  315. f"You cannot define a route with the same specificity as a optional catch-all route ('{route}' and '{new_route}')"
  316. )
  317. route_catchall = catchall_in_route(route)
  318. if (
  319. route_catchall
  320. and newroute_catchall
  321. and catchall_prefix(route) == catchall_prefix(new_route)
  322. ):
  323. raise ValueError(
  324. f"You cannot use multiple catchall for the same dynamic route ({route} !== {new_route})"
  325. )
  326. def add_custom_404_page(
  327. self,
  328. component,
  329. title=None,
  330. image=None,
  331. description=None,
  332. meta=constants.DEFAULT_META_LIST,
  333. ):
  334. """Define a custom 404 page for any url having no match.
  335. If there is no page defined on 'index' route, add the 404 page to it.
  336. If there is no global catchall defined, add the 404 page with a catchall
  337. Args:
  338. component: The component to display at the page.
  339. title: The title of the page.
  340. description: The description of the page.
  341. image: The image to display on the page.
  342. meta: The metadata of the page.
  343. """
  344. title = title or constants.TITLE_404
  345. image = image or constants.FAVICON_404
  346. description = description or constants.DESCRIPTION_404
  347. component = component if isinstance(component, Component) else component()
  348. compiler_utils.add_meta(
  349. component,
  350. title=title,
  351. image=image,
  352. description=description,
  353. meta=meta,
  354. )
  355. froute = format.format_route
  356. self.pages[froute(constants.SLUG_404)] = component
  357. def setup_admin_dash(self):
  358. """Setup the admin dash."""
  359. # Get the config.
  360. config = get_config()
  361. if config.admin_dash and config.admin_dash.models:
  362. # Build the admin dashboard
  363. admin = (
  364. config.admin_dash.admin
  365. if config.admin_dash.admin
  366. else Admin(
  367. engine=Model.get_db_engine(),
  368. title="Reflex Admin Dashboard",
  369. logo_url="https://pynecone.io/logo.png",
  370. )
  371. )
  372. for model in config.admin_dash.models:
  373. view = config.admin_dash.view_overrides.get(model, ModelView)
  374. admin.add_view(view(model))
  375. admin.mount_to(self.api)
  376. def compile(self):
  377. """Compile the app and output it to the pages folder."""
  378. # Create a progress bar.
  379. progress = Progress(
  380. *Progress.get_default_columns()[:-1],
  381. MofNCompleteColumn(),
  382. TimeElapsedColumn(),
  383. )
  384. task = progress.add_task("Compiling: ", total=len(self.pages))
  385. for render, kwargs in DECORATED_ROUTES:
  386. self.add_page(render, **kwargs)
  387. # Get the env mode.
  388. config = get_config()
  389. # Empty the .web pages directory
  390. compiler.purge_web_pages_dir()
  391. # Store the compile results.
  392. compile_results = []
  393. # Compile the pages in parallel.
  394. custom_components = set()
  395. thread_pool = ThreadPool()
  396. with progress:
  397. for route, component in self.pages.items():
  398. progress.advance(task)
  399. component.add_style(self.style)
  400. compile_results.append(
  401. thread_pool.apply_async(
  402. compiler.compile_page,
  403. args=(
  404. route,
  405. component,
  406. self.state,
  407. self.connect_error_component,
  408. ),
  409. )
  410. )
  411. # Add the custom components from the page to the set.
  412. custom_components |= component.get_custom_components()
  413. thread_pool.close()
  414. thread_pool.join()
  415. # Get the results.
  416. compile_results = [result.get() for result in compile_results]
  417. # Compile the custom components.
  418. compile_results.append(compiler.compile_components(custom_components))
  419. # Compile the root document with base styles and fonts.
  420. compile_results.append(compiler.compile_document_root(self.stylesheets))
  421. # Compile the theme.
  422. compile_results.append(compiler.compile_theme(self.style))
  423. # Compile the Tailwind config.
  424. if config.tailwind is not None:
  425. config.tailwind["content"] = config.tailwind.get(
  426. "content", constants.TAILWIND_CONTENT
  427. )
  428. compile_results.append(compiler.compile_tailwind(config.tailwind))
  429. # Write the pages at the end to trigger the NextJS hot reload only once.
  430. thread_pool = ThreadPool()
  431. for output_path, code in compile_results:
  432. compiler_utils.write_page(output_path, code)
  433. thread_pool.apply_async(compiler_utils.write_page, args=(output_path, code))
  434. thread_pool.close()
  435. thread_pool.join()
  436. async def process(
  437. app: App, event: Event, sid: str, headers: Dict, client_ip: str
  438. ) -> AsyncIterator[StateUpdate]:
  439. """Process an event.
  440. Args:
  441. app: The app to process the event for.
  442. event: The event to process.
  443. sid: The Socket.IO session id.
  444. headers: The client headers.
  445. client_ip: The client_ip.
  446. Yields:
  447. The state updates after processing the event.
  448. """
  449. # Get the state for the session.
  450. state = app.state_manager.get_state(event.token)
  451. # Add request data to the state.
  452. router_data = event.router_data
  453. router_data.update(
  454. {
  455. constants.RouteVar.QUERY: format.format_query_params(event.router_data),
  456. constants.RouteVar.CLIENT_TOKEN: event.token,
  457. constants.RouteVar.SESSION_ID: sid,
  458. constants.RouteVar.HEADERS: headers,
  459. constants.RouteVar.CLIENT_IP: client_ip,
  460. }
  461. )
  462. # re-assign only when the value is different
  463. if state.router_data != router_data:
  464. # assignment will recurse into substates and force recalculation of
  465. # dependent ComputedVar (dynamic route variables)
  466. state.router_data = router_data
  467. # Preprocess the event.
  468. update = await app.preprocess(state, event)
  469. # If there was an update, yield it.
  470. if update is not None:
  471. yield update
  472. # Only process the event if there is no update.
  473. else:
  474. # Process the event.
  475. async for update in state._process(event):
  476. # Postprocess the event.
  477. update = await app.postprocess(state, event, update)
  478. # Yield the update.
  479. yield update
  480. # Set the state for the session.
  481. app.state_manager.set_state(event.token, state)
  482. async def ping() -> str:
  483. """Test API endpoint.
  484. Returns:
  485. The response.
  486. """
  487. return "pong"
  488. def upload(app: App):
  489. """Upload a file.
  490. Args:
  491. app: The app to upload the file for.
  492. Returns:
  493. The upload function.
  494. """
  495. async def upload_file(files: List[UploadFile]):
  496. """Upload a file.
  497. Args:
  498. files: The file(s) to upload.
  499. Raises:
  500. ValueError: if there are no args with supported annotation.
  501. """
  502. assert files[0].filename is not None
  503. token, handler = files[0].filename.split(":")[:2]
  504. for file in files:
  505. assert file.filename is not None
  506. file.filename = file.filename.split(":")[-1]
  507. # Get the state for the session.
  508. state = app.state_manager.get_state(token)
  509. # get the current session ID
  510. sid = state.get_sid()
  511. # get the current state(parent state/substate)
  512. path = handler.split(".")[:-1]
  513. current_state = state.get_substate(path)
  514. handler_upload_param: Tuple = ()
  515. # get handler function
  516. func = getattr(current_state, handler.split(".")[-1])
  517. # check if there exists any handler args with annotation, List[UploadFile]
  518. for k, v in inspect.getfullargspec(
  519. func.fn if isinstance(func, EventHandler) else func
  520. ).annotations.items():
  521. if types.is_generic_alias(v) and types._issubclass(
  522. v.__args__[0], UploadFile
  523. ):
  524. handler_upload_param = (k, v)
  525. break
  526. if not handler_upload_param:
  527. raise ValueError(
  528. f"`{handler}` handler should have a parameter annotated as List["
  529. f"rx.UploadFile]"
  530. )
  531. event = Event(
  532. token=token,
  533. name=handler,
  534. payload={handler_upload_param[0]: files},
  535. )
  536. async for update in state._process(event):
  537. # Postprocess the event.
  538. update = await app.postprocess(state, event, update)
  539. # Send update to client
  540. await asyncio.create_task(
  541. app.event_namespace.emit(str(constants.SocketEvent.EVENT), update.json(), to=sid) # type: ignore
  542. )
  543. # Set the state for the session.
  544. app.state_manager.set_state(event.token, state)
  545. return upload_file
  546. class EventNamespace(AsyncNamespace):
  547. """The event namespace."""
  548. # The application object.
  549. app: App
  550. def __init__(self, namespace: str, app: App):
  551. """Initialize the event namespace.
  552. Args:
  553. namespace: The namespace.
  554. app: The application object.
  555. """
  556. super().__init__(namespace)
  557. self.app = app
  558. def on_connect(self, sid, environ):
  559. """Event for when the websocket disconnects.
  560. Args:
  561. sid: The Socket.IO session id.
  562. environ: The request information, including HTTP headers.
  563. """
  564. pass
  565. def on_disconnect(self, sid):
  566. """Event for when the websocket disconnects.
  567. Args:
  568. sid: The Socket.IO session id.
  569. """
  570. pass
  571. async def on_event(self, sid, data):
  572. """Event for receiving front-end websocket events.
  573. Args:
  574. sid: The Socket.IO session id.
  575. data: The event data.
  576. """
  577. # Get the event.
  578. event = Event.parse_raw(data)
  579. # Get the event environment.
  580. assert self.app.sio is not None
  581. environ = self.app.sio.get_environ(sid, self.namespace)
  582. assert environ is not None
  583. # Get the client headers.
  584. headers = {
  585. k.decode("utf-8"): v.decode("utf-8")
  586. for (k, v) in environ["asgi.scope"]["headers"]
  587. }
  588. # Get the client IP
  589. client_ip = environ["REMOTE_ADDR"]
  590. # Process the events.
  591. async for update in process(self.app, event, sid, headers, client_ip):
  592. # Emit the event.
  593. await asyncio.create_task(
  594. self.emit(str(constants.SocketEvent.EVENT), update.json(), to=sid)
  595. )
  596. async def on_ping(self, sid):
  597. """Event for testing the API endpoint.
  598. Args:
  599. sid: The Socket.IO session id.
  600. """
  601. # Emit the test event.
  602. await self.emit(str(constants.SocketEvent.PING), "pong", to=sid)