|
6 | 6 | from sanic import Sanic, response |
7 | 7 |
|
8 | 8 | import idom |
9 | | -from idom.config import IDOM_CLIENT_IMPORT_SOURCE_URL |
| 9 | +from idom.client.manage import web_modules_dir |
10 | 10 | from idom.server.sanic import PerClientStateServer |
11 | 11 | from idom.widgets.utils import multiview |
12 | 12 |
|
13 | 13 |
|
| 14 | +HERE = Path(__file__).parent |
14 | 15 | IDOM_MODEL_SERVER_URL_PREFIX = "/_idom" |
15 | 16 |
|
16 | | -IDOM_CLIENT_IMPORT_SOURCE_URL.set_default( |
17 | | - # set_default because scripts/live_docs.py needs to overwrite this |
18 | | - f"{IDOM_MODEL_SERVER_URL_PREFIX}{IDOM_CLIENT_IMPORT_SOURCE_URL.default}" |
19 | | -) |
20 | 17 |
|
| 18 | +def make_app(): |
| 19 | + app = Sanic(__name__) |
| 20 | + app.static("/docs", str(HERE / "build")) |
| 21 | + app.static("/_modules", str(web_modules_dir())) |
21 | 22 |
|
22 | | -here = Path(__file__).parent |
| 23 | + @app.route("/") |
| 24 | + async def forward_to_index(request): |
| 25 | + return response.redirect("/docs/index.html") |
23 | 26 |
|
24 | | -app = Sanic(__name__) |
25 | | -app.static("/docs", str(here / "build")) |
| 27 | + return app |
26 | 28 |
|
27 | 29 |
|
28 | | -@app.route("/") |
29 | | -async def forward_to_index(request): |
30 | | - return response.redirect("/docs/index.html") |
| 30 | +def make_component(): |
| 31 | + mount, component = multiview() |
31 | 32 |
|
| 33 | + examples_dir = HERE / "source" / "examples" |
| 34 | + sys.path.insert(0, str(examples_dir)) |
32 | 35 |
|
33 | | -mount, component = multiview() |
| 36 | + original_run = idom.run |
| 37 | + try: |
| 38 | + for file in examples_dir.iterdir(): |
| 39 | + if ( |
| 40 | + not file.is_file() |
| 41 | + or not file.suffix == ".py" |
| 42 | + or file.stem.startswith("_") |
| 43 | + ): |
| 44 | + continue |
34 | 45 |
|
35 | | -examples_dir = here / "source" / "examples" |
36 | | -sys.path.insert(0, str(examples_dir)) |
| 46 | + # Modify the run function so when we exec the file |
| 47 | + # instead of running a server we mount the view. |
| 48 | + idom.run = partial(mount.add, file.stem) |
37 | 49 |
|
| 50 | + with file.open() as f: |
| 51 | + try: |
| 52 | + exec( |
| 53 | + f.read(), |
| 54 | + { |
| 55 | + "__file__": str(file), |
| 56 | + "__name__": f"__main__.examples.{file.stem}", |
| 57 | + }, |
| 58 | + ) |
| 59 | + except Exception as error: |
| 60 | + raise RuntimeError(f"Failed to execute {file}") from error |
| 61 | + finally: |
| 62 | + idom.run = original_run |
38 | 63 |
|
39 | | -original_run = idom.run |
40 | | -try: |
41 | | - for file in examples_dir.iterdir(): |
42 | | - if not file.is_file() or not file.suffix == ".py" or file.stem.startswith("_"): |
43 | | - continue |
44 | | - |
45 | | - # Modify the run function so when we exec the file |
46 | | - # instead of running a server we mount the view. |
47 | | - idom.run = partial(mount.add, file.stem) |
48 | | - |
49 | | - with file.open() as f: |
50 | | - try: |
51 | | - exec( |
52 | | - f.read(), |
53 | | - { |
54 | | - "__file__": str(file), |
55 | | - "__name__": f"__main__.examples.{file.stem}", |
56 | | - }, |
57 | | - ) |
58 | | - except Exception as error: |
59 | | - raise RuntimeError(f"Failed to execute {file}") from error |
60 | | -finally: |
61 | | - idom.run = original_run |
62 | | - |
63 | | - |
64 | | -PerClientStateServer( |
65 | | - component, |
66 | | - { |
67 | | - "redirect_root_to_index": False, |
68 | | - "url_prefix": IDOM_MODEL_SERVER_URL_PREFIX, |
69 | | - }, |
70 | | - app, |
71 | | -) |
| 64 | + return component |
72 | 65 |
|
73 | 66 |
|
74 | 67 | if __name__ == "__main__": |
| 68 | + app = make_app() |
| 69 | + |
| 70 | + PerClientStateServer( |
| 71 | + make_component(), |
| 72 | + { |
| 73 | + "redirect_root_to_index": False, |
| 74 | + "url_prefix": IDOM_MODEL_SERVER_URL_PREFIX, |
| 75 | + }, |
| 76 | + app, |
| 77 | + ) |
| 78 | + |
75 | 79 | app.run( |
76 | 80 | host="0.0.0.0", |
77 | 81 | port=int(os.environ.get("PORT", 5000)), |
|
0 commit comments