From 65a5aaa6d83560cf2ab94dd0dd4d9b300bf6cd45 Mon Sep 17 00:00:00 2001 From: Darin Crawmer Date: Wed, 2 Jul 2025 08:47:22 -0700 Subject: [PATCH 1/2] add random button --- 00_game_of_life/main.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/00_game_of_life/main.py b/00_game_of_life/main.py index d5d73cf..f455c35 100644 --- a/00_game_of_life/main.py +++ b/00_game_of_life/main.py @@ -1,6 +1,7 @@ from fasthtml.common import * import asyncio import sys +import random if __name__ == "__main__": sys.exit("Run this app with `uvicorn main:app`") @@ -52,8 +53,9 @@ def Home(): gol = Div(Grid(), id='gol', cls='row center-xs') run_btn = Button('Run', id='run', cls='col-xs-2', hx_put='/run', hx_target='#gol', hx_swap='none') pause_btn = Button('Pause', id='pause', cls='col-xs-2', hx_put='/pause', hx_target='#gol', hx_swap='none') + random_btn = Button('Random', id='random', cls='col-xs-2', hx_put='/random', hx_target='#gol', hx_swap='none') reset_btn = Button('Reset', id='reset', cls='col-xs-2', hx_put='/reset', hx_target='#gol', hx_swap='none') - main = Main(gol, Div(run_btn, pause_btn, reset_btn, cls='row center-xs'), hx_ext="ws", ws_connect="/gol") + main = Main(gol, Div(run_btn, pause_btn, random_btn, reset_btn, cls='row center-xs'), hx_ext="ws", ws_connect="/gol") 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'))) return Title('Game of Life'), main, footer @@ -90,6 +92,11 @@ async def put(): game_state['running'] = True await update_players() +@rt("/random") +async def put(): + game_state['grid'] = [[random.randint(0, 1) for _ in range(20)] for _ in range(20)] + await update_players() + @rt("/reset") async def put(): game_state['grid'] = [[0 for _ in range(20)] for _ in range(20)] From 9dba364fb1c4a21e0f5eca441b26c83b94db7ff8 Mon Sep 17 00:00:00 2001 From: Darin Crawmer Date: Wed, 2 Jul 2025 08:59:17 -0700 Subject: [PATCH 2/2] fix link --- 00_game_of_life/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/00_game_of_life/main.py b/00_game_of_life/main.py index f455c35..38d9e81 100644 --- a/00_game_of_life/main.py +++ b/00_game_of_life/main.py @@ -56,7 +56,7 @@ def Home(): random_btn = Button('Random', id='random', cls='col-xs-2', hx_put='/random', hx_target='#gol', hx_swap='none') reset_btn = Button('Reset', id='reset', cls='col-xs-2', hx_put='/reset', hx_target='#gol', hx_swap='none') main = Main(gol, Div(run_btn, pause_btn, random_btn, reset_btn, cls='row center-xs'), hx_ext="ws", ws_connect="/gol") - 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'))) + footer = Footer(P('Made by Nathan Cooper. Check out the code', AX('here', href='https://github.com/AnswerDotAI/fasthtml-example/tree/main/00_game_of_life', target='_blank'))) return Title('Game of Life'), main, footer @rt('/')