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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -35
app.py CHANGED
@@ -236,6 +236,16 @@ def setup_figure(env):
236
 
237
  return fig
238
 
 
 
 
 
 
 
 
 
 
 
239
  # Streamlit app
240
  st.title("Advanced Cell Evolution Simulation")
241
 
@@ -246,50 +256,52 @@ update_interval = st.slider("Update interval (seconds)", 0.1, 5.0, 1.0)
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
 
 
 
 
255
  for _ in range(initial_cells):
256
- cell = Cell(random.uniform(0, env.width), random.uniform(0, env.height))
257
- env.add_cell(cell)
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
 
236
 
237
  return fig
238
 
239
+ import random
240
+ import numpy as np
241
+ import streamlit as st
242
+ import plotly.graph_objects as go
243
+ from plotly.subplots import make_subplots
244
+ import time
245
+ import threading
246
+
247
+ # Your classes and functions...
248
+
249
  # Streamlit app
250
  st.title("Advanced Cell Evolution Simulation")
251
 
 
256
  chart_placeholder = st.empty()
257
 
258
  # Create a button to start/stop the simulation
259
+ if 'run_simulation' not in st.session_state:
260
+ st.session_state.run_simulation = False
261
+ if 'env' not in st.session_state:
262
+ st.session_state.env = None
263
+ if 'fig' not in st.session_state:
264
+ st.session_state.fig = None
265
+
266
+ def start_simulation():
267
+ st.session_state.run_simulation = True
268
+ st.session_state.env = Environment(100, 100)
269
  for _ in range(initial_cells):
270
+ cell = Cell(random.uniform(0, st.session_state.env.width), random.uniform(0, st.session_state.env.height))
271
+ st.session_state.env.add_cell(cell)
272
+ st.session_state.fig = setup_figure(st.session_state.env)
273
+ thread = threading.Thread(target=run_simulation)
274
+ thread.start()
275
+
276
+ def stop_simulation():
277
+ st.session_state.run_simulation = False
278
+
279
+ start_button = st.button("Start Simulation", on_click=start_simulation)
280
+ stop_button = st.button("Stop Simulation", on_click=stop_simulation)
281
+
282
+ def run_simulation():
283
+ while st.session_state.run_simulation:
284
+ st.session_state.env.update()
285
 
286
+ with st.session_state.fig.batch_update():
287
+ cell_data, population_history = st.session_state.env.get_visualization_data()
288
  for i, (cell_type, data) in enumerate(cell_data.items()):
289
+ st.session_state.fig.data[i].x = data["x"]
290
+ st.session_state.fig.data[i].y = data["y"]
291
+ st.session_state.fig.data[i].marker.size = data["size"]
292
 
293
  for i, (cell_type, counts) in enumerate(population_history.items()):
294
+ st.session_state.fig.data[i+5].y = counts # +5 because we have 5 cell types in the first subplot
295
  if cell_type != "modified" and cell_type != "plant_like":
296
+ st.session_state.fig.data[i+10].y = counts # Update individual population charts
297
  else:
298
+ st.session_state.fig.data[13].y = population_history["plant_like"]
299
+ st.session_state.fig.data[14].y = population_history["modified"]
300
 
301
+ st.session_state.fig.layout.title.text = f"Advanced Cell Evolution Simulation (Time: {st.session_state.env.time})"
302
 
303
  # Update the chart
304
+ chart_placeholder.plotly_chart(st.session_state.fig, use_container_width=True)
305
 
306
  # Wait for the specified interval
307
+ time.sleep(update_interval)