|
| 1 | +import sys |
| 2 | +from pathlib import Path |
| 3 | +from traceback import print_exc |
| 4 | + |
| 5 | +import idom |
| 6 | +from idom.server.sanic import PerClientStateServer |
| 7 | + |
| 8 | +here = Path(__file__).parent |
| 9 | +examples_dir = here.parent / "docs" / "source" / "examples" |
| 10 | +sys.path.insert(0, str(examples_dir)) |
| 11 | + |
| 12 | +for file in examples_dir.iterdir(): |
| 13 | + if not file.is_file() or not file.suffix == ".py" or file.stem.startswith("_"): |
| 14 | + continue |
| 15 | + |
| 16 | + |
| 17 | +def main(): |
| 18 | + views = [] |
| 19 | + |
| 20 | + for example_file in examples_dir.glob("*.py"): |
| 21 | + if not example_file.stem.startswith("_"): |
| 22 | + with example_file.open() as f_obj: |
| 23 | + try: |
| 24 | + exec( |
| 25 | + f_obj.read(), |
| 26 | + { |
| 27 | + "display": lambda f, *a, **kw: views.append( |
| 28 | + (example_file.stem, f, a, kw) |
| 29 | + ), |
| 30 | + "__file__": str(file), |
| 31 | + "__name__": f"widgets.{file.stem}", |
| 32 | + }, |
| 33 | + ) |
| 34 | + except Exception: |
| 35 | + print(f"Failed to load {example_file}") |
| 36 | + print_exc() |
| 37 | + print() |
| 38 | + |
| 39 | + @idom.element |
| 40 | + def AllExamples(): |
| 41 | + examples = [] |
| 42 | + for title, f, a, kw in views: |
| 43 | + examples.append(idom.html.h1(title)) |
| 44 | + examples.append(f(*a, **kw)) |
| 45 | + examples.append(idom.html.hr({"style": {"margin-top": "20px"}})) |
| 46 | + return idom.html.div({"style": {"margin": "20px"}}, examples) |
| 47 | + |
| 48 | + PerClientStateServer(AllExamples).run("127.0.0.1", 5000) |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + main() |
0 commit comments