Skip to content

Commit 65a5aaa

Browse files
committed
add random button
1 parent 34d1471 commit 65a5aaa

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

00_game_of_life/main.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from fasthtml.common import *
22
import asyncio
33
import sys
4+
import random
45

56
if __name__ == "__main__": sys.exit("Run this app with `uvicorn main:app`")
67

@@ -52,8 +53,9 @@ def Home():
5253
gol = Div(Grid(), id='gol', cls='row center-xs')
5354
run_btn = Button('Run', id='run', cls='col-xs-2', hx_put='/run', hx_target='#gol', hx_swap='none')
5455
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')
5557
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")
5759
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')))
5860
return Title('Game of Life'), main, footer
5961

@@ -90,6 +92,11 @@ async def put():
9092
game_state['running'] = True
9193
await update_players()
9294

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+
93100
@rt("/reset")
94101
async def put():
95102
game_state['grid'] = [[0 for _ in range(20)] for _ in range(20)]

0 commit comments

Comments
 (0)