Skip to content

Commit

Permalink
fix docs + improve doc exammple runner
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorshea committed Sep 4, 2020
1 parent 49bb80f commit 547acde
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 59 deletions.
6 changes: 3 additions & 3 deletions docs/source/examples/simple_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def RandomWalkGraph(mu, sigma):
interval = use_interval(0.5)
data, set_data = idom.hooks.use_state([{"x": 0, "y": 0}] * 50)

@idom.hooks.use_async
@idom.hooks.use_effect
async def animate():
await interval
last_data_point = data[-1]
Expand Down Expand Up @@ -75,8 +75,8 @@ def update_value(value):
)


def use_interval(rate: float) -> Awaitable[None]:
usage_time = use_ref(time.time())
def use_interval(rate):
usage_time = idom.hooks.use_ref(time.time())

async def interval() -> None:
await asyncio.sleep(rate - (time.time() - usage_time.current))
Expand Down
6 changes: 3 additions & 3 deletions docs/source/examples/snake_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async def on_direction_change(event):

interval = use_interval(0.5)

@idom.hooks.use_async
@idom.hooks.use_effect
async def animate():
if new_game_state is not None:
await asyncio.sleep(1)
Expand Down Expand Up @@ -114,8 +114,8 @@ def set_food():
return food, set_food


def use_interval(rate: float) -> Awaitable[None]:
usage_time = use_ref(time.time())
def use_interval(rate):
usage_time = idom.hooks.use_ref(time.time())

async def interval() -> None:
await asyncio.sleep(rate - (time.time() - usage_time.current))
Expand Down
53 changes: 0 additions & 53 deletions scripts/run_doc_example.py

This file was deleted.

52 changes: 52 additions & 0 deletions scripts/run_doc_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import sys
from pathlib import Path
from traceback import print_exc

import idom
from idom.server.sanic import PerClientStateServer

here = Path(__file__).parent
examples_dir = here.parent / "docs" / "source" / "examples"
sys.path.insert(0, str(examples_dir))

for file in examples_dir.iterdir():
if not file.is_file() or not file.suffix == ".py" or file.stem.startswith("_"):
continue


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:
exec(
f_obj.read(),
{
"display": lambda f, *a, **kw: views.append(
(example_file.stem, f, a, kw)
),
"__file__": str(file),
"__name__": f"widgets.{file.stem}",
},
)
except Exception:
print(f"Failed to load {example_file}")
print_exc()
print()

@idom.element
def AllExamples():
examples = []
for title, f, a, kw in views:
examples.append(idom.html.h1(title))
examples.append(f(*a, **kw))
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)


if __name__ == "__main__":
main()

0 comments on commit 547acde

Please sign in to comment.