Sephfox commited on
Commit
4e0890b
·
verified ·
1 Parent(s): fce2463

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -31
app.py CHANGED
@@ -239,11 +239,16 @@ def setup_figure(env):
239
  # Streamlit app
240
  st.title("Advanced Cell Evolution Simulation")
241
 
242
- num_steps = st.slider("Number of simulation steps", 100, 2000, 1000)
243
  initial_cells = st.slider("Initial number of cells", 10, 200, 100)
244
- update_interval = st.slider("Update interval (steps)", 1, 50, 10)
245
 
246
- if st.button("Run Simulation"):
 
 
 
 
 
 
247
  env = Environment(100, 100)
248
 
249
  # Add initial cells
@@ -253,38 +258,38 @@ if st.button("Run Simulation"):
253
 
254
  # Set up the figure
255
  fig = setup_figure(env)
256
- chart = st.plotly_chart(fig, use_container_width=True)
257
-
258
- # Create a progress bar
259
- progress_bar = st.progress(0)
260
 
261
  # Run simulation
262
- for step in range(num_steps):
263
  env.update()
264
 
265
- # Update the figure data and chart less frequently
266
- if step % update_interval == 0 or step == num_steps - 1:
267
- with fig.batch_update():
268
- cell_data, population_history = env.get_visualization_data()
269
- for i, (cell_type, data) in enumerate(cell_data.items()):
270
- fig.data[i].x = data["x"]
271
- fig.data[i].y = data["y"]
272
- fig.data[i].marker.size = data["size"]
273
-
274
- for i, (cell_type, counts) in enumerate(population_history.items()):
275
- fig.data[i+5].y = counts # +5 because we have 5 cell types in the first subplot
276
- if cell_type != "modified" and cell_type != "plant_like":
277
- fig.data[i+10].y = counts # Update individual population charts
278
- else:
279
- fig.data[13].y = population_history["plant_like"]
280
- fig.data[14].y = population_history["modified"]
281
-
282
- fig.layout.title.text = f"Advanced Cell Evolution Simulation (Time: {env.time})"
283
 
284
- # Update the chart
285
- chart.plotly_chart(fig, use_container_width=True)
 
 
 
 
 
286
 
287
- # Update progress bar
288
- progress_bar.progress((step + 1) / num_steps)
 
 
 
 
 
 
 
 
 
289
 
290
- st.write("Simulation complete!")
 
 
 
239
  # Streamlit app
240
  st.title("Advanced Cell Evolution Simulation")
241
 
 
242
  initial_cells = st.slider("Initial number of cells", 10, 200, 100)
243
+ update_interval = st.slider("Update interval (seconds)", 0.1, 5.0, 1.0)
244
 
245
+ # Create a placeholder for the chart
246
+ chart_placeholder = st.empty()
247
+
248
+ # Create a button to start/stop the simulation
249
+ start_button = st.button("Start Simulation")
250
+
251
+ if start_button:
252
  env = Environment(100, 100)
253
 
254
  # Add initial cells
 
258
 
259
  # Set up the figure
260
  fig = setup_figure(env)
 
 
 
 
261
 
262
  # Run simulation
263
+ while True:
264
  env.update()
265
 
266
+ with fig.batch_update():
267
+ cell_data, population_history = env.get_visualization_data()
268
+ for i, (cell_type, data) in enumerate(cell_data.items()):
269
+ fig.data[i].x = data["x"]
270
+ fig.data[i].y = data["y"]
271
+ fig.data[i].marker.size = data["size"]
 
 
 
 
 
 
 
 
 
 
 
 
272
 
273
+ for i, (cell_type, counts) in enumerate(population_history.items()):
274
+ fig.data[i+5].y = counts # +5 because we have 5 cell types in the first subplot
275
+ if cell_type != "modified" and cell_type != "plant_like":
276
+ fig.data[i+10].y = counts # Update individual population charts
277
+ else:
278
+ fig.data[13].y = population_history["plant_like"]
279
+ fig.data[14].y = population_history["modified"]
280
 
281
+ fig.layout.title.text = f"Advanced Cell Evolution Simulation (Time: {env.time})"
282
+
283
+ # Update the chart
284
+ chart_placeholder.plotly_chart(fig, use_container_width=True)
285
+
286
+ # Wait for the specified interval
287
+ time.sleep(update_interval)
288
+
289
+ # Check if the Streamlit script should stop
290
+ if not st.session_state.get('run_simulation', True):
291
+ break
292
 
293
+ # Add a button to stop the simulation
294
+ if st.button("Stop Simulation"):
295
+ st.session_state.run_simulation = False