app.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. """The main Pynecone app."""
  2. import asyncio
  3. import inspect
  4. from typing import Any, Callable, Coroutine, Dict, List, Optional, Tuple, Type, Union
  5. from fastapi import FastAPI, UploadFile
  6. from fastapi.middleware import cors
  7. from socketio import ASGIApp, AsyncNamespace, AsyncServer
  8. from pynecone import constants
  9. from pynecone.base import Base
  10. from pynecone.compiler import compiler
  11. from pynecone.compiler import utils as compiler_utils
  12. from pynecone.components.component import Component, ComponentStyle
  13. from pynecone.config import get_config
  14. from pynecone.event import Event, EventHandler
  15. from pynecone.middleware import HydrateMiddleware, Middleware
  16. from pynecone.model import Model
  17. from pynecone.route import (
  18. DECORATED_ROUTES,
  19. catchall_in_route,
  20. catchall_prefix,
  21. get_route_args,
  22. verify_route_validity,
  23. )
  24. from pynecone.state import DefaultState, Delta, State, StateManager, StateUpdate
  25. from pynecone.utils import format, types
  26. # Define custom types.
  27. ComponentCallable = Callable[[], Component]
  28. Reducer = Callable[[Event], Coroutine[Any, Any, StateUpdate]]
  29. class App(Base):
  30. """A Pynecone application."""
  31. # A map from a page route to the component to render.
  32. pages: Dict[str, Component] = {}
  33. # A list of URLs to stylesheets to include in the app.
  34. stylesheets: List[str] = []
  35. # The backend API object.
  36. api: FastAPI = None # type: ignore
  37. # The Socket.IO AsyncServer.
  38. sio: Optional[AsyncServer] = None
  39. # The socket app.
  40. socket_app: Optional[ASGIApp] = None
  41. # The state class to use for the app.
  42. state: Type[State] = DefaultState
  43. # Class to manage many client states.
  44. state_manager: StateManager = StateManager()
  45. # The styling to apply to each component.
  46. style: ComponentStyle = {}
  47. # Middleware to add to the app.
  48. middleware: List[Middleware] = []
  49. # Event handlers to trigger when a page loads.
  50. load_events: Dict[str, Union[EventHandler, List[EventHandler]]] = {}
  51. def __init__(self, *args, **kwargs):
  52. """Initialize the app.
  53. Args:
  54. *args: Args to initialize the app with.
  55. **kwargs: Kwargs to initialize the app with.
  56. """
  57. super().__init__(*args, **kwargs)
  58. # Get the config
  59. config = get_config()
  60. # Add middleware.
  61. self.middleware.append(HydrateMiddleware())
  62. # Set up the state manager.
  63. self.state_manager.setup(state=self.state)
  64. # Set up the API.
  65. self.api = FastAPI()
  66. self.add_cors()
  67. self.add_default_endpoints()
  68. # Set up CORS options.
  69. cors_allowed_origins = config.cors_allowed_origins
  70. if config.cors_allowed_origins == [constants.CORS_ALLOWED_ORIGINS]:
  71. cors_allowed_origins = "*"
  72. # Set up the Socket.IO AsyncServer.
  73. self.sio = AsyncServer(
  74. async_mode="asgi",
  75. cors_allowed_origins=cors_allowed_origins,
  76. cors_credentials=config.cors_credentials,
  77. max_http_buffer_size=config.polling_max_http_buffer_size,
  78. ping_interval=constants.PING_INTERVAL,
  79. ping_timeout=constants.PING_TIMEOUT,
  80. )
  81. # Create the socket app. Note event endpoint constant replaces the default 'socket.io' path.
  82. self.socket_app = ASGIApp(self.sio, socketio_path="")
  83. # Create the event namespace and attach the main app. Not related to any paths.
  84. event_namespace = EventNamespace("/event", self)
  85. # Register the event namespace with the socket.
  86. self.sio.register_namespace(event_namespace)
  87. # Mount the socket app with the API.
  88. self.api.mount(str(constants.Endpoint.EVENT), self.socket_app)
  89. def __repr__(self) -> str:
  90. """Get the string representation of the app.
  91. Returns:
  92. The string representation of the app.
  93. """
  94. return f"<App state={self.state.__name__}>"
  95. def __call__(self) -> FastAPI:
  96. """Run the backend api instance.
  97. Returns:
  98. The backend api.
  99. """
  100. return self.api
  101. def add_default_endpoints(self):
  102. """Add the default endpoints."""
  103. # To test the server.
  104. self.api.get(str(constants.Endpoint.PING))(ping)
  105. # To upload files.
  106. self.api.post(str(constants.Endpoint.UPLOAD))(upload(self))
  107. def add_cors(self):
  108. """Add CORS middleware to the app."""
  109. self.api.add_middleware(
  110. cors.CORSMiddleware,
  111. allow_credentials=True,
  112. allow_methods=["*"],
  113. allow_headers=["*"],
  114. allow_origins=["*"],
  115. )
  116. async def preprocess(
  117. self, state: State, event: Event
  118. ) -> Optional[Union[StateUpdate, List[StateUpdate]]]:
  119. """Preprocess the event.
  120. This is where middleware can modify the event before it is processed.
  121. Each middleware is called in the order it was added to the app.
  122. If a middleware returns a delta, the event is not processed and the
  123. delta is returned.
  124. Args:
  125. state: The state to preprocess.
  126. event: The event to preprocess.
  127. Returns:
  128. An optional state to return.
  129. """
  130. for middleware in self.middleware:
  131. if asyncio.iscoroutinefunction(middleware.preprocess):
  132. out = await middleware.preprocess(app=self, state=state, event=event)
  133. else:
  134. out = middleware.preprocess(app=self, state=state, event=event)
  135. if out is not None:
  136. return out # type: ignore
  137. async def postprocess(
  138. self, state: State, event: Event, delta: Delta
  139. ) -> Optional[Delta]:
  140. """Postprocess the event.
  141. This is where middleware can modify the delta after it is processed.
  142. Each middleware is called in the order it was added to the app.
  143. If a middleware returns a delta, the delta is not processed and the
  144. delta is returned.
  145. Args:
  146. state: The state to postprocess.
  147. event: The event to postprocess.
  148. delta: The delta to postprocess.
  149. Returns:
  150. An optional state to return.
  151. """
  152. for middleware in self.middleware:
  153. if asyncio.iscoroutinefunction(middleware.postprocess):
  154. out = await middleware.postprocess(
  155. app=self, state=state, event=event, delta=delta
  156. )
  157. else:
  158. out = middleware.postprocess(
  159. app=self, state=state, event=event, delta=delta
  160. )
  161. if out is not None:
  162. return out # type: ignore
  163. def add_middleware(self, middleware: Middleware, index: Optional[int] = None):
  164. """Add middleware to the app.
  165. Args:
  166. middleware: The middleware to add.
  167. index: The index to add the middleware at.
  168. """
  169. if index is None:
  170. self.middleware.append(middleware)
  171. else:
  172. self.middleware.insert(index, middleware)
  173. def add_page(
  174. self,
  175. component: Union[Component, ComponentCallable],
  176. route: Optional[str] = None,
  177. title: str = constants.DEFAULT_TITLE,
  178. description: str = constants.DEFAULT_DESCRIPTION,
  179. image=constants.DEFAULT_IMAGE,
  180. on_load: Optional[Union[EventHandler, List[EventHandler]]] = None,
  181. meta: List[Dict] = constants.DEFAULT_META_LIST,
  182. script_tags: Optional[List[Component]] = None,
  183. ):
  184. """Add a page to the app.
  185. If the component is a callable, by default the route is the name of the
  186. function. Otherwise, a route must be provided.
  187. Args:
  188. component: The component to display at the page.
  189. route: The route to display the component at.
  190. title: The title of the page.
  191. description: The description of the page.
  192. image: The image to display on the page.
  193. on_load: The event handler(s) that will be called each time the page load.
  194. meta: The metadata of the page.
  195. script_tags: List of script tags to be added to component
  196. Raises:
  197. TypeError: If an invalid var operation is used.
  198. """
  199. # If the route is not set, get it from the callable.
  200. if route is None:
  201. assert isinstance(
  202. component, Callable
  203. ), "Route must be set if component is not a callable."
  204. route = component.__name__
  205. # Check if the route given is valid
  206. verify_route_validity(route)
  207. # Apply dynamic args to the route.
  208. self.state.setup_dynamic_args(get_route_args(route))
  209. # Generate the component if it is a callable.
  210. try:
  211. component = component if isinstance(component, Component) else component()
  212. except TypeError as e:
  213. message = str(e)
  214. if "BaseVar" in message or "ComputedVar" in message:
  215. raise TypeError(
  216. "You may be trying to use an invalid Python function on a state var. "
  217. "When referencing a var inside your render code, only limited var operations are supported. "
  218. "See the var operation docs here: https://pynecone.io/docs/state/vars "
  219. ) from e
  220. raise e
  221. # Add meta information to the component.
  222. compiler_utils.add_meta(
  223. component, title=title, image=image, description=description, meta=meta
  224. )
  225. # Add script tags if given
  226. if script_tags:
  227. component.children.extend(script_tags)
  228. # Format the route.
  229. route = format.format_route(route)
  230. # Add the page.
  231. self._check_routes_conflict(route)
  232. self.pages[route] = component
  233. if on_load:
  234. self.load_events[route] = on_load
  235. def _check_routes_conflict(self, new_route: str):
  236. """Verify if there is any conflict between the new route and any existing route.
  237. Based on conflicts that NextJS would throw if not intercepted.
  238. Raises:
  239. ValueError: exception showing which conflict exist with the route to be added
  240. Args:
  241. new_route: the route being newly added.
  242. """
  243. newroute_catchall = catchall_in_route(new_route)
  244. if not newroute_catchall:
  245. return
  246. for route in self.pages:
  247. route = "" if route == "index" else route
  248. if new_route.startswith(f"{route}/[[..."):
  249. raise ValueError(
  250. f"You cannot define a route with the same specificity as a optional catch-all route ('{route}' and '{new_route}')"
  251. )
  252. route_catchall = catchall_in_route(route)
  253. if (
  254. route_catchall
  255. and newroute_catchall
  256. and catchall_prefix(route) == catchall_prefix(new_route)
  257. ):
  258. raise ValueError(
  259. f"You cannot use multiple catchall for the same dynamic route ({route} !== {new_route})"
  260. )
  261. def add_custom_404_page(
  262. self,
  263. component,
  264. title=None,
  265. image=None,
  266. description=None,
  267. meta=constants.DEFAULT_META_LIST,
  268. ):
  269. """Define a custom 404 page for any url having no match.
  270. If there is no page defined on 'index' route, add the 404 page to it.
  271. If there is no global catchall defined, add the 404 page with a catchall
  272. Args:
  273. component: The component to display at the page.
  274. title: The title of the page.
  275. description: The description of the page.
  276. image: The image to display on the page.
  277. meta: The metadata of the page.
  278. """
  279. title = title or constants.TITLE_404
  280. image = image or constants.FAVICON_404
  281. description = description or constants.DESCRIPTION_404
  282. component = component if isinstance(component, Component) else component()
  283. compiler_utils.add_meta(
  284. component, title=title, image=image, description=description, meta=meta
  285. )
  286. froute = format.format_route
  287. if (froute(constants.ROOT_404) not in self.pages) and (
  288. not any(page.startswith("[[...") for page in self.pages)
  289. ):
  290. self.pages[froute(constants.ROOT_404)] = component
  291. if not any(
  292. page.startswith("[...") or page.startswith("[[...") for page in self.pages
  293. ):
  294. self.pages[froute(constants.SLUG_404)] = component
  295. def compile(self, force_compile: bool = False):
  296. """Compile the app and output it to the pages folder.
  297. If the config environment is set to production, the app will
  298. not be compiled.
  299. Args:
  300. force_compile: Whether to force the app to compile.
  301. """
  302. for render, kwargs in DECORATED_ROUTES:
  303. self.add_page(render, **kwargs)
  304. # Get the env mode.
  305. config = get_config()
  306. if config.env != constants.Env.DEV and not force_compile:
  307. print("Skipping compilation in non-dev mode.")
  308. return
  309. # Update models during hot reload.
  310. if config.db_url is not None:
  311. Model.create_all()
  312. # Empty the .web pages directory
  313. compiler.purge_web_pages_dir()
  314. # Compile the root document with base styles and fonts.
  315. compiler.compile_document_root(self.stylesheets)
  316. # Compile the theme.
  317. compiler.compile_theme(self.style)
  318. # Compile the pages.
  319. custom_components = set()
  320. for route, component in self.pages.items():
  321. component.add_style(self.style)
  322. compiler.compile_page(route, component, self.state)
  323. # Add the custom components from the page to the set.
  324. custom_components |= component.get_custom_components()
  325. # Compile the custom components.
  326. compiler.compile_components(custom_components)
  327. async def process(
  328. app: App, event: Event, sid: str, headers: Dict, client_ip: str
  329. ) -> List[StateUpdate]:
  330. """Process an event.
  331. Args:
  332. app: The app to process the event for.
  333. event: The event to process.
  334. sid: The Socket.IO session id.
  335. headers: The client headers.
  336. client_ip: The client_ip.
  337. Returns:
  338. The state updates after processing the event.
  339. """
  340. # Get the state for the session.
  341. state = app.state_manager.get_state(event.token)
  342. # Add request data to the state.
  343. state.router_data = event.router_data
  344. state.router_data.update(
  345. {
  346. constants.RouteVar.QUERY: format.format_query_params(event.router_data),
  347. constants.RouteVar.CLIENT_TOKEN: event.token,
  348. constants.RouteVar.SESSION_ID: sid,
  349. constants.RouteVar.HEADERS: headers,
  350. constants.RouteVar.CLIENT_IP: client_ip,
  351. }
  352. )
  353. # Also pass router_data to all substates. (TODO: this isn't recursive currently)
  354. for _, substate in state.substates.items():
  355. substate.router_data = state.router_data
  356. # Preprocess the event.
  357. pre = await app.preprocess(state, event)
  358. if isinstance(pre, StateUpdate):
  359. return [pre]
  360. updates = pre
  361. # Apply the event to the state.
  362. if updates is None:
  363. updates = [await state._process(event)]
  364. app.state_manager.set_state(event.token, state)
  365. # Postprocess the event.
  366. post_list = []
  367. for update in updates:
  368. post = await app.postprocess(state, event, update.delta) # type: ignore
  369. post_list.append(post) if post else None
  370. if len(post_list) > 0:
  371. return [StateUpdate(delta=post) for post in post_list]
  372. # Return the updates.
  373. return updates
  374. async def ping() -> str:
  375. """Test API endpoint.
  376. Returns:
  377. The response.
  378. """
  379. return "pong"
  380. def upload(app: App):
  381. """Upload a file.
  382. Args:
  383. app: The app to upload the file for.
  384. Returns:
  385. The upload function.
  386. """
  387. async def upload_file(files: List[UploadFile]):
  388. """Upload a file.
  389. Args:
  390. files: The file(s) to upload.
  391. Returns:
  392. The state update after processing the event.
  393. Raises:
  394. ValueError: if there are no args with supported annotation.
  395. """
  396. token, handler = files[0].filename.split(":")[:2]
  397. for file in files:
  398. file.filename = file.filename.split(":")[-1]
  399. # Get the state for the session.
  400. state = app.state_manager.get_state(token)
  401. handler_upload_param: Tuple = ()
  402. # get handler function
  403. func = getattr(state, handler.split(".")[-1])
  404. # check if there exists any handler args with annotation, List[UploadFile]
  405. for k, v in inspect.getfullargspec(
  406. func.fn if isinstance(func, EventHandler) else func
  407. ).annotations.items():
  408. if types.is_generic_alias(v) and types._issubclass(
  409. v.__args__[0], UploadFile
  410. ):
  411. handler_upload_param = (k, v)
  412. break
  413. if not handler_upload_param:
  414. raise ValueError(
  415. f"`{handler}` handler should have a parameter annotated as List["
  416. f"pc.UploadFile]"
  417. )
  418. event = Event(
  419. token=token,
  420. name=handler,
  421. payload={handler_upload_param[0]: files},
  422. )
  423. update = await state._process(event)
  424. return update
  425. return upload_file
  426. class EventNamespace(AsyncNamespace):
  427. """The event namespace."""
  428. # The application object.
  429. app: App
  430. def __init__(self, namespace: str, app: App):
  431. """Initialize the event namespace.
  432. Args:
  433. namespace: The namespace.
  434. app: The application object.
  435. """
  436. super().__init__(namespace)
  437. self.app = app
  438. def on_connect(self, sid, environ):
  439. """Event for when the websocket disconnects.
  440. Args:
  441. sid: The Socket.IO session id.
  442. environ: The request information, including HTTP headers.
  443. """
  444. pass
  445. def on_disconnect(self, sid):
  446. """Event for when the websocket disconnects.
  447. Args:
  448. sid: The Socket.IO session id.
  449. """
  450. pass
  451. async def on_event(self, sid, data):
  452. """Event for receiving front-end websocket events.
  453. Args:
  454. sid: The Socket.IO session id.
  455. data: The event data.
  456. """
  457. # Get the event.
  458. event = Event.parse_raw(data)
  459. # Get the event environment.
  460. assert self.app.sio is not None
  461. environ = self.app.sio.get_environ(sid, self.namespace)
  462. assert environ is not None
  463. # Get the client headers.
  464. headers = {
  465. k.decode("utf-8"): v.decode("utf-8")
  466. for (k, v) in environ["asgi.scope"]["headers"]
  467. }
  468. # Get the client IP
  469. client_ip = environ["REMOTE_ADDR"]
  470. # Process the events.
  471. updates = await process(self.app, event, sid, headers, client_ip)
  472. # Emit the event.
  473. for update in updates:
  474. await self.emit(str(constants.SocketEvent.EVENT), update.json(), to=sid) # type: ignore
  475. async def on_ping(self, sid):
  476. """Event for testing the API endpoint.
  477. Args:
  478. sid: The Socket.IO session id.
  479. """
  480. # Emit the test event.
  481. await self.emit(str(constants.SocketEvent.PING), "pong", to=sid)