def install(cls, packages, ignore_installed=False, fallback=None): """ Installs specified packages into application directory. Arguments --------- packages: list or tuple The packages to install from npm ignored_installed: boolean Whether to ignore if the package was previously installed. fallback: str or idom.component The fallback to display while the component is loading """ import idom from idom.config import IDOM_CLIENT_BUILD_DIR idom_dist_dir = DIST_DIR / "idom" idom_build_dir = idom_dist_dir / "build" if not idom_build_dir.is_dir(): idom_build_dir.mkdir() shutil.copyfile(idom_dist_dir / 'package.json', idom_build_dir / 'package.json') if IDOM_CLIENT_BUILD_DIR.get() != idom_build_dir: IDOM_CLIENT_BUILD_DIR.set(idom_build_dir) # just in case packages were already installed but the build hasn't been # copied over to DIST_DIR yet. ignore_installed = True return idom.install(packages, ignore_installed, fallback)
def _setup_blueprint_routes(self, config: Config, blueprint: Blueprint) -> None: """Add routes to the application blueprint""" @blueprint.websocket("/stream") # type: ignore async def model_stream(request: request.Request, socket: WebSocketCommonProtocol) -> None: async def sock_send(value: LayoutUpdate) -> None: await socket.send(json.dumps(value)) async def sock_recv() -> LayoutEvent: return LayoutEvent(**json.loads(await socket.recv())) component_params = {k: request.args.get(k) for k in request.args} await self._run_dispatcher(sock_send, sock_recv, component_params) if config["serve_static_files"]: blueprint.static("/client", str(IDOM_CLIENT_BUILD_DIR.get())) if config["redirect_root_to_index"]: @blueprint.route("/") # type: ignore def redirect_to_index( request: request.Request, ) -> response.HTTPResponse: return response.redirect( f"{blueprint.url_prefix}/client/index.html?{request.query_string}" )
def _setup_static_files(self, config: Config, app: FastAPI) -> None: # This really should be added to the APIRouter, but there's a bug in FastAPI # BUG: https://github.com/tiangolo/fastapi/issues/1469 url_prefix = config["url_prefix"] if config["serve_static_files"]: app.mount( f"{url_prefix}/client", StaticFiles( directory=str(IDOM_CLIENT_BUILD_DIR.get()), html=True, check_dir=True, ), name="idom_static_files", ) if config["redirect_root_to_index"]: @app.route(f"{url_prefix}/") def redirect_to_index(request: Request) -> RedirectResponse: return RedirectResponse( f"{url_prefix}/client/index.html?{request.query_params}" )
def _create_route_handlers(self, config: Config) -> _RouteHandlerSpecs: handlers: _RouteHandlerSpecs = [( "/stream", self._model_stream_handler_type, { "component_constructor": self._root_component_constructor }, )] if config["serve_static_files"]: handlers.append(( r"/client/(.*)", StaticFileHandler, { "path": str(IDOM_CLIENT_BUILD_DIR.get()) }, )) if config["redirect_root_to_index"]: handlers.append(("/", RedirectHandler, { "url": "./client/index.html" })) return handlers
def build_dependencies() -> Dict[str, str]: package_json = IDOM_CLIENT_BUILD_DIR.get() / "package.json" return cast(Dict[str, str], json.loads(package_json.read_text())["dependencies"])
def replace_build_dir(source: Path) -> None: target = IDOM_CLIENT_BUILD_DIR.get() if target.exists(): shutil.rmtree(target) shutil.copytree(source, target, symlinks=True)
def restore_build_dir_from_backup() -> None: target = IDOM_CLIENT_BUILD_DIR.get() if target.exists(): shutil.rmtree(target) shutil.copytree(BACKUP_BUILD_DIR, target, symlinks=True)
def web_modules_dir() -> Path: return IDOM_CLIENT_BUILD_DIR.get().joinpath( *IDOM_CLIENT_IMPORT_SOURCE_URL_INFIX[1:].split("/"))
from idom.config import IDOM_CLIENT_BUILD_DIR HERE = Path(__file__).parent APP_DIR = HERE / "app" BACKUP_BUILD_DIR = APP_DIR / "build" # the path relative to the build that contains import sources IDOM_CLIENT_IMPORT_SOURCE_URL_INFIX = "/_snowpack/pkg" def web_modules_dir() -> Path: return IDOM_CLIENT_BUILD_DIR.get().joinpath( *IDOM_CLIENT_IMPORT_SOURCE_URL_INFIX[1:].split("/")) if not IDOM_CLIENT_BUILD_DIR.get().exists(): # pragma: no cover shutil.copytree(BACKUP_BUILD_DIR, IDOM_CLIENT_BUILD_DIR.get(), symlinks=True) def restore_build_dir_from_backup() -> None: target = IDOM_CLIENT_BUILD_DIR.get() if target.exists(): shutil.rmtree(target) shutil.copytree(BACKUP_BUILD_DIR, target, symlinks=True) def replace_build_dir(source: Path) -> None: target = IDOM_CLIENT_BUILD_DIR.get() if target.exists():
def send_build_dir(path: str) -> Any: return send_from_directory(str(IDOM_CLIENT_BUILD_DIR.get()), path)