Skip to content

Add a Random button to Game of Life in FastHTML #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions 00_game_of_life/main.py
Original file line number Diff line number Diff line change
@@ -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`")

Expand Down Expand Up @@ -52,9 +53,10 @@ 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")
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')))
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/00_game_of_life', target='_blank')))
return Title('Game of Life'), main, footer

@rt('/')
Expand Down Expand Up @@ -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)]
Expand Down