Sephfox commited on
Commit
68b5b1a
·
verified ·
1 Parent(s): d6b2d77

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -49
app.py CHANGED
@@ -4,6 +4,10 @@ import streamlit as st
4
  import plotly.graph_objects as go
5
  from plotly.subplots import make_subplots
6
  import time
 
 
 
 
7
 
8
  class Organelle:
9
  def __init__(self, type):
@@ -236,15 +240,8 @@ def setup_figure(env):
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")
@@ -255,53 +252,56 @@ update_interval = st.slider("Update interval (seconds)", 0.1, 5.0, 1.0)
255
  # Create a placeholder for the chart
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)
 
4
  import plotly.graph_objects as go
5
  from plotly.subplots import make_subplots
6
  import time
7
+ import threading
8
+ import queue
9
+
10
+ # Your classes and functions...
11
 
12
  class Organelle:
13
  def __init__(self, type):
 
240
 
241
  return fig
242
 
243
+ # Create a queue to communicate between the threads
244
+ q = queue.Queue()
 
 
 
 
 
 
 
245
 
246
  # Streamlit app
247
  st.title("Advanced Cell Evolution Simulation")
 
252
  # Create a placeholder for the chart
253
  chart_placeholder = st.empty()
254
 
255
+ # Create a function to run the simulation
256
+ def run_simulation():
257
+ env = Environment(100, 100)
258
+ for _ in range(initial_cells):
259
+ cell = Cell(random.uniform(0, env.width), random.uniform(0, env.height))
260
+ env.add_cell(cell)
261
+ fig = setup_figure(env)
262
+
263
+ while True:
264
+ try:
265
+ q.get(timeout=0.1)
266
+ break
267
+ except queue.Empty:
268
+ pass
269
+
270
+ env.update()
271
+
272
+ with fig.batch_update():
273
+ cell_data, population_history = env.get_visualization_data()
274
+ for i, (cell_type, data) in enumerate(cell_data.items()):
275
+ fig.data[i].x = data["x"]
276
+ fig.data[i].y = data["y"]
277
+ fig.data[i].marker.size = data["size"]
278
+
279
+ for i, (cell_type, counts) in enumerate(population_history.items()):
280
+ fig.data[i+5].y = counts # +5 because we have 5 cell types in the first subplot
281
+ if cell_type != "modified" and cell_type != "plant_like":
282
+ fig.data[i+10].y = counts # Update individual population charts
283
+ else:
284
+ fig.data[13].y = population_history["plant_like"]
285
+ fig.data[14].y = population_history["modified"]
286
+
287
+ q.put(fig)
288
 
289
+ # Create a function to start the simulation
290
  def start_simulation():
 
 
 
 
 
 
291
  thread = threading.Thread(target=run_simulation)
292
  thread.start()
293
 
294
+ # Create a function to stop the simulation
295
  def stop_simulation():
296
+ q.put(None)
297
 
298
  start_button = st.button("Start Simulation", on_click=start_simulation)
299
  stop_button = st.button("Stop Simulation", on_click=stop_simulation)
300
 
301
+ while True:
302
+ try:
303
+ fig = q.get(timeout=0.1)
304
+ chart_placeholder.plotly_chart(fig, use_container_width=True)
305
+ except queue.Empty:
306
+ time.sleep(0.1)
307
+ continue