|
1 | 1 | from fasthtml.common import * |
2 | 2 | import asyncio |
3 | 3 | import sys |
| 4 | +import random |
4 | 5 |
|
5 | 6 | if __name__ == "__main__": sys.exit("Run this app with `uvicorn main:app`") |
6 | 7 |
|
@@ -52,8 +53,9 @@ def Home(): |
52 | 53 | gol = Div(Grid(), id='gol', cls='row center-xs') |
53 | 54 | run_btn = Button('Run', id='run', cls='col-xs-2', hx_put='/run', hx_target='#gol', hx_swap='none') |
54 | 55 | pause_btn = Button('Pause', id='pause', cls='col-xs-2', hx_put='/pause', hx_target='#gol', hx_swap='none') |
| 56 | + random_btn = Button('Random', id='random', cls='col-xs-2', hx_put='/random', hx_target='#gol', hx_swap='none') |
55 | 57 | reset_btn = Button('Reset', id='reset', cls='col-xs-2', hx_put='/reset', hx_target='#gol', hx_swap='none') |
56 | | - main = Main(gol, Div(run_btn, pause_btn, reset_btn, cls='row center-xs'), hx_ext="ws", ws_connect="/gol") |
| 58 | + main = Main(gol, Div(run_btn, pause_btn, random_btn, reset_btn, cls='row center-xs'), hx_ext="ws", ws_connect="/gol") |
57 | 59 | footer = Footer(P('Made by Nathan Cooper. Check out the code', AX('here', href='https://github.com/AnswerDotAI/fasthtml-example/tree/main/game_of_life', target='_blank'))) |
58 | 60 | return Title('Game of Life'), main, footer |
59 | 61 |
|
@@ -90,6 +92,11 @@ async def put(): |
90 | 92 | game_state['running'] = True |
91 | 93 | await update_players() |
92 | 94 |
|
| 95 | +@rt("/random") |
| 96 | +async def put(): |
| 97 | + game_state['grid'] = [[random.randint(0, 1) for _ in range(20)] for _ in range(20)] |
| 98 | + await update_players() |
| 99 | + |
93 | 100 | @rt("/reset") |
94 | 101 | async def put(): |
95 | 102 | game_state['grid'] = [[0 for _ in range(20)] for _ in range(20)] |
|
0 commit comments