gokaymeydan commited on
Commit
5f86dcd
·
1 Parent(s): bd2c426

add plotly

Browse files
Files changed (1) hide show
  1. app.py +73 -12
app.py CHANGED
@@ -1,8 +1,9 @@
1
- import streamlit as st
2
  import random
3
- import time
4
- from algorithms import insertion_sort
5
  import matplotlib.pyplot as plt
 
 
 
6
 
7
  st.title("Insertion Sort Visualizer")
8
 
@@ -11,19 +12,79 @@ option = st.radio("Choose input method:", ["Manual Input", "Random List"])
11
  if option == "Manual Input":
12
  user_input = st.text_input("Enter numbers seperated by commas (like 5,3,1,4)")
13
  if user_input:
14
- data = list(map(int, user_input.split(',')))
15
  else:
16
- length = st.slider("List length", 5,20,8)
17
- data = random.sample(range(1,30), length)
18
 
19
- if 'data' in locals() and st.button("Visualize Insertion Sort"):
20
  steps = insertion_sort(data)
21
 
22
  st.write(f"Total steps: {len(steps)}")
23
 
 
24
  for i, step in enumerate(steps):
25
- fig, ax = plt.subplots()
26
- ax.bar(range(len(step)), step)
27
- ax.set_title(f"Step {i + 1}")
28
- st.pyplot(fig)
29
- time.sleep(0.3)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import random
2
+ import plotly.graph_objects as go
 
3
  import matplotlib.pyplot as plt
4
+ import streamlit as st
5
+
6
+ from algorithms import insertion_sort
7
 
8
  st.title("Insertion Sort Visualizer")
9
 
 
12
  if option == "Manual Input":
13
  user_input = st.text_input("Enter numbers seperated by commas (like 5,3,1,4)")
14
  if user_input:
15
+ data = list(map(int, user_input.split(",")))
16
  else:
17
+ length = st.slider("List length", 5, 20, 8)
18
+ data = random.sample(range(1, 30), length)
19
 
20
+ if "data" in locals() and st.button("Visualize Insertion Sort"):
21
  steps = insertion_sort(data)
22
 
23
  st.write(f"Total steps: {len(steps)}")
24
 
25
+ frames = []
26
  for i, step in enumerate(steps):
27
+ array = step["array"]
28
+ active_index = step["active_index"]
29
+ sorted_boundary = step["sorted_boundary"]
30
+
31
+ colors = []
32
+ for j in range(len(array)):
33
+ if j == active_index:
34
+ colors.append("red")
35
+ elif j <= sorted_boundary:
36
+ colors.append("green")
37
+ else:
38
+ colors.append("gray")
39
+
40
+ frames.append(go.Frame(
41
+ data=[go.Scatter(
42
+ x=list(range(len(array))),
43
+ y=array,
44
+ mode="markers+text",
45
+ marker=dict(size=40, color=colors),
46
+ text=array,
47
+ textposition="middle center"
48
+ )],
49
+ name=f"Step {i+1}"
50
+ ))
51
+ initial = steps[0]
52
+ initial_colors = [
53
+ "red" if j == initial["active_index"] else
54
+ "green" if j <= initial["sorted_boundary"] else "gray"
55
+ for j in range(len(initial["array"]))
56
+ ]
57
+
58
+ fig = go.Figure(
59
+ data=[go.Scatter(
60
+ x=list(range(len(initial["array"]))),
61
+ y=initial["array"],
62
+ mode="markers+text",
63
+ marker=dict(size=40, color=initial_colors),
64
+ text=initial["array"],
65
+ textposition="middle center"
66
+ )],
67
+ layout=go.Layout(
68
+ title="Insertion Sort Animation",
69
+ xaxis=dict(range=[-0.5, len(initial["array"]) - 0.5]),
70
+ yaxis=dict(range=[0, max(max(s['array']) for s in steps) + 5]),
71
+ updatemenus=[dict(
72
+ type="buttons",
73
+ buttons=[dict(label="Play", method="animate", args=[None])],
74
+ showactive=False
75
+ )],
76
+ sliders=[{
77
+ "steps": [{
78
+ "args": [[f"Step {i+1}"], {"frame": {"duration": 500, "redraw": True}}],
79
+ "label": f"{i+1}",
80
+ "method": "animate"
81
+ } for i in range(len(frames))],
82
+ "transition": {"duration": 0},
83
+ "x": 0, "y": -0.1,
84
+ "currentvalue": {"prefix": "Step: "}
85
+ }]
86
+ ),
87
+ frames=frames
88
+ )
89
+
90
+ st.plotly_chart(fig)