def new_builder(): [s.stop() for s in _running_idom_servers] from docs import main importlib.reload(main) server = PerClientStateServer(main.component, {"cors": True}) _running_idom_servers.append(server) server.daemon("127.0.0.1", 5555, debug=True) old_builder()
def new_builder(): [s.stop() for s in _running_idom_servers] # we need to set this before `docs.main` does IDOM_CLIENT_IMPORT_SOURCE_URL.set( f"http://{example_server_host}{IDOM_CLIENT_IMPORT_SOURCE_URL.default}" ) from docs import main importlib.reload(main) server = PerClientStateServer(main.component, {"cors": True}) _running_idom_servers.append(server) server.daemon("127.0.0.1", 5555, debug=True) old_builder()
def main(): views = [] for example_file in examples_dir.glob("*.py"): if not example_file.stem.startswith("_"): with example_file.open() as f_obj: try: idom.run = lambda func: views.append( (example_file.stem, func)) exec( f_obj.read(), { "__file__": str(file), "__name__": f"__main__.examples.{file.stem}", }, ) except Exception: print(f"Failed to load {example_file}") print_exc() print() @idom.component def AllExamples(): examples = [] for title, func in views: examples.append(idom.html.h1(title)) examples.append(func()) examples.append(idom.html.hr({"style": {"margin-top": "20px"}})) return idom.html.div({"style": {"margin": "20px"}}, examples) PerClientStateServer(AllExamples).run("127.0.0.1", 5000)
def run() -> None: arguments = docopt(__doc__, version="0.1.0") for k, v in DEFAULTS.items(): if arguments[k] is None: arguments[k] = v for k, v in CAST.items(): arguments[k] = v(arguments[k]) for old_k, new_k in REMAP.items(): arguments[new_k] = arguments.pop(old_k) app = Sanic() app.static("_static", str(HERE / "static")) idom_server = PerClientStateServer( lambda: Slidedeck( int(arguments["start_at_slide_number"]), arguments["filepath"] ) ) idom_server.register(app) run_options = {"host": arguments["host"], "port": arguments["port"]} if not arguments["no_auto_open"]: thread = idom_server.daemon(**run_options) webbrowser.open(f"http://{run_options['host']}:{run_options['port']}") thread.join() else: idom_server.run(**run_options)
try: exec( f.read(), { "__file__": str(file), "__name__": f"__main__.examples.{file.stem}", }, ) except Exception as error: raise RuntimeError(f"Failed to execute {file}") from error finally: idom.run = original_run PerClientStateServer( component, { "redirect_root_to_index": False, "url_prefix": IDOM_MODEL_SERVER_URL_PREFIX, }, ).register(app) if __name__ == "__main__": app.run( host="0.0.0.0", port=int(os.environ.get("PORT", 5000)), workers=int(os.environ.get("WEB_CONCURRENCY", 1)), debug=bool(int(os.environ.get("DEBUG", "0"))), )
def run() -> None: raw_arguments = docopt(__doc__, version=slidedown.__version__) filepath = Path(raw_arguments.pop("<filepath>")) projpath = filepath.parent if not filepath.exists(): print(f"No such file: '{filepath}'") exit(1) config_file = filepath.parent / "slidedown.json" if config_file.exists(): config_data = json.loads(config_file.read_text()) if not isinstance(config_data, dict): print(f"Invalid config {config_file}") exit(1) else: config_data = {} processes_arguments: Dict[str, Any] = {} for arg, spec in list(ARG_SPECS.items()): raw_value = raw_arguments[arg] if not raw_value and config_data.get(spec.name): del raw_arguments[arg] value = config_data[spec.name] elif raw_arguments[arg] is None: del raw_arguments[arg] value = spec.default else: value = raw_arguments.pop(arg) if value is UNDEFINED: raise ValueError(f"Internal failure - {arg} requires a value") if spec.cast is not None: try: value = spec.cast(value) except Exception: print(f"Invalid value for {arg}") exit(1) processes_arguments[spec.name] = value if raw_arguments: raise RuntimeError( f"Internal failure - unconsumed arguments {raw_arguments}") arguments = Arguments(**processes_arguments) if arguments.show_options: print(json.dumps(arguments._asdict(), indent=2, sort_keys=True)) app = Sanic() theme_path = arguments.theme if not theme_path.is_absolute(): theme_path = projpath / theme_path if theme_path.is_file(): app.static("_static/markdown.css", str(arguments.theme)) else: print(f"Stylesheet {theme_path} does not exist") exit(1) idom_server = PerClientStateServer( lambda: Slidedeck(arguments.start_slide, filepath), app=app, ) files_did_change = _make_file_did_change_callback( filepath, arguments.reload_watch, arguments.reload_ignore, ) _run_server( idom_server, { "host": arguments.host, "port": arguments.port }, not arguments.no_browser, None if arguments.no_reload else files_did_change, arguments.reload_delay, )
# Modify the run function so when we exec the file # instead of running a server we mount the view. idom.run = mount[file.stem] with file.open() as f: try: exec( f.read(), { "__file__": str(file), "__name__": f"__main__.examples.{file.stem}", }, ) except Exception as error: raise RuntimeError(f"Failed to execute {file}") from error finally: idom.run = original_run PerClientStateServer(component, { "redirect_root_to_index": False }).register(app) if __name__ == "__main__": app.run( host="0.0.0.0", port=int(os.environ.get("PORT", 5000)), workers=int(os.environ.get("WEB_CONCURRENCY", 1)), debug=bool(int(os.environ.get("DEBUG", "0"))), )
mount, element = multiview() here = Path(__file__).parent widgets = here / "source" / "widgets" sys.path.insert(0, str(widgets)) for file in widgets.iterdir(): if not file.is_file() or file.suffix != ".py" or file.stem.startswith("_"): continue with file.open() as f: exec( f.read(), { "display": getattr(mount, file.stem), "__file__": str(file), "__name__": f"widgets.{file.stem}", }, ) PerClientStateServer(element).configure({"redirect_root_to_index": False}).register(app) if __name__ == "__main__": app.run( host="0.0.0.0", port=int(os.environ.get("PORT", 5000)), workers=int(os.environ.get("WEB_CONCURRENCY", 1)), debug={"true": True, "false": False}[os.environ.get("DEBUG", "False").lower()], )