Skip to content

Commit d6bb878

Browse files
committed
port game of life to streamlit
1 parent 2a03852 commit d6bb878

File tree

1 file changed

+8
-17
lines changed
  • examples/conways_game_of_life

1 file changed

+8
-17
lines changed

examples/conways_game_of_life/app.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import random
2020

2121
model = st.title('Boltzman Wealth Model')
22-
# num_agents= st.slider('Choose how many agents to include in the model',min_value=1,max_value=100,value=50)
2322
num_ticks = st.slider('Select number of Simulation Runs',min_value=1,max_value=100,value=50)
2423
height = st.slider('Select Grid Height',min_value=10,max_value=100,step=10,value=15)
2524
width = st.slider('Select Grid Width',min_value=10,max_value=100,step=10,value=20)
@@ -37,7 +36,6 @@
3736
step =0
3837
# init grid
3938
df_grid= pd.DataFrame()
40-
# df_gini = pd.DataFrame({'step':[0],'gini':[-1]})
4139
agent_counts = np.zeros((model.grid.width, model.grid.height))
4240
for x in range(width):
4341
for y in range(height):
@@ -53,34 +51,27 @@
5351
my_bar = st.progress(0, text='Simulation Progress') # progress
5452
placeholder = st.empty()
5553
st.subheader('Agent Grid')
56-
chart = st.altair_chart(heatmap)
54+
chart = st.altair_chart(heatmap,use_container_width=True)
5755
color_scale = alt.Scale(domain=[0,1],
58-
range=['red', 'green'])
56+
range=['red', 'yellow'])
5957
for i in range(num_ticks):
6058
model.step()
6159
my_bar.progress((i/num_ticks), text='Simulation progress')
6260
placeholder.text('Step = %d' % i)
63-
for cell in model.grid.coord_iter():
64-
cell_content, x, y = cell
61+
for contents, x,y in model.grid.coord_iter():
62+
# print('x:',x,'y:',y, 'state:',contents)
6563
selected_row = df_grid[(df_grid['x'] == x) & (df_grid['y'] == y)]
66-
df_grid.loc[selected_row.index, 'state'] = cell.state #random.choice([1,2])
67-
68-
# df_gini = pd.concat([df_gini , pd.DataFrame({'step':[i],'gini':[model.datacollector.model_vars['Gini'][i]]})])
69-
# st.table(df_grid)
64+
df_grid.loc[selected_row.index, 'state'] = contents.state #random.choice([1,2])
65+
7066
heatmap = alt.Chart(df_grid).mark_circle(size=100).encode(
7167
x='x',
7268
y ='y',
7369
color = alt.Color('state',scale=color_scale)).interactive().properties(width=800, height=600)
7470
chart.altair_chart(heatmap)
7571

76-
time.sleep(0.01)
72+
time.sleep(0.1)
7773

7874

7975
tock = time.time()
8076
st.success('Simulation completed in {:.2f} secs'.format(tock-tick))
81-
82-
# st.subheader('Agent Grid')
83-
# fig = px.imshow(agent_counts,labels={'color':'Agent Count'})
84-
# st.plotly_chart(fig)
85-
# st.subheader('Gini value over sim ticks (Plotly)')
86-
# chart = st.line_chart(model.datacollector.model_vars['Gini'])
77+

0 commit comments

Comments
 (0)