shukdevdatta123 commited on
Commit
1b9621d
·
verified ·
1 Parent(s): 73af586

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -14
app.py CHANGED
@@ -1,6 +1,9 @@
1
  import streamlit as st
2
  import time
 
 
3
  from PIL import Image
 
4
 
5
  # Inject Custom CSS for Aesthetic Enhancements
6
  def inject_custom_css():
@@ -18,11 +21,9 @@ def inject_custom_css():
18
  box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
19
  border-radius: 12px;
20
  }
21
-
22
  .css-1d391kg .sidebar-content {
23
  padding: 2rem;
24
  }
25
-
26
  /* Title and Headers */
27
  h1 {
28
  font-size: 2.5rem;
@@ -45,11 +46,9 @@ def inject_custom_css():
45
  box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
46
  cursor: pointer;
47
  }
48
-
49
  .css-1emrehy:hover {
50
  background-color: #ff7b7b;
51
  }
52
-
53
  /* Text input styling */
54
  .css-1bppn4z input {
55
  border: 2px solid #ddd;
@@ -66,7 +65,6 @@ def inject_custom_css():
66
  font-family: 'Courier New', monospace;
67
  color: #333333;
68
  }
69
-
70
  /* Image styling */
71
  img {
72
  border-radius: 8px;
@@ -76,7 +74,7 @@ def inject_custom_css():
76
  </style>
77
  """, unsafe_allow_html=True)
78
 
79
- # Bubble Sort Algorithm with animation
80
  def bubble_sort(arr):
81
  steps = []
82
  n = len(arr)
@@ -87,7 +85,7 @@ def bubble_sort(arr):
87
  steps.append(list(arr)) # Record the state of the array after each step
88
  return steps
89
 
90
- # Insertion Sort Algorithm with animation
91
  def insertion_sort(arr):
92
  steps = []
93
  for i in range(1, len(arr)):
@@ -101,7 +99,7 @@ def insertion_sort(arr):
101
  steps.append(list(arr)) # Record the state after inserting the key
102
  return steps
103
 
104
- # Selection Sort Algorithm with animation
105
  def selection_sort(arr):
106
  steps = []
107
  n = len(arr)
@@ -114,11 +112,37 @@ def selection_sort(arr):
114
  steps.append(list(arr)) # Record the state after each swap
115
  return steps
116
 
117
- # Function to handle animation in the streamlit app
118
- def animate_sorting(steps):
 
119
  for step in steps:
120
- st.write(step)
121
- time.sleep(0.5) # Adjust sleep time to control animation speed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
 
123
  # Streamlit App Interface
124
  def main():
@@ -159,8 +183,8 @@ def main():
159
  elif algorithm == "Selection Sort":
160
  steps = selection_sort(arr)
161
 
162
- # Animate the sorting process
163
- animate_sorting(steps)
164
 
165
  # Display the final sorted array
166
  st.write("Sorted Array:", arr)
 
1
  import streamlit as st
2
  import time
3
+ import plotly.graph_objs as go
4
+ import plotly.express as px
5
  from PIL import Image
6
+ import numpy as np
7
 
8
  # Inject Custom CSS for Aesthetic Enhancements
9
  def inject_custom_css():
 
21
  box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
22
  border-radius: 12px;
23
  }
 
24
  .css-1d391kg .sidebar-content {
25
  padding: 2rem;
26
  }
 
27
  /* Title and Headers */
28
  h1 {
29
  font-size: 2.5rem;
 
46
  box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
47
  cursor: pointer;
48
  }
 
49
  .css-1emrehy:hover {
50
  background-color: #ff7b7b;
51
  }
 
52
  /* Text input styling */
53
  .css-1bppn4z input {
54
  border: 2px solid #ddd;
 
65
  font-family: 'Courier New', monospace;
66
  color: #333333;
67
  }
 
68
  /* Image styling */
69
  img {
70
  border-radius: 8px;
 
74
  </style>
75
  """, unsafe_allow_html=True)
76
 
77
+ # Bubble Sort Algorithm with Plotly animation
78
  def bubble_sort(arr):
79
  steps = []
80
  n = len(arr)
 
85
  steps.append(list(arr)) # Record the state of the array after each step
86
  return steps
87
 
88
+ # Insertion Sort Algorithm with Plotly animation
89
  def insertion_sort(arr):
90
  steps = []
91
  for i in range(1, len(arr)):
 
99
  steps.append(list(arr)) # Record the state after inserting the key
100
  return steps
101
 
102
+ # Selection Sort Algorithm with Plotly animation
103
  def selection_sort(arr):
104
  steps = []
105
  n = len(arr)
 
112
  steps.append(list(arr)) # Record the state after each swap
113
  return steps
114
 
115
+ # Plotly animation function to display sorting process
116
+ def plotly_animation(steps, algorithm_name):
117
+ fig = go.Figure()
118
  for step in steps:
119
+ fig.add_trace(go.Scatter(
120
+ x=list(range(len(step))),
121
+ y=step,
122
+ mode="lines+markers",
123
+ name=f'{algorithm_name} Sorting Step',
124
+ line=dict(color='blue'),
125
+ marker=dict(color='red', size=10),
126
+ ))
127
+ fig.update_layout(
128
+ title=f"{algorithm_name} Algorithm - Sorting Process",
129
+ xaxis_title="Index",
130
+ yaxis_title="Value",
131
+ updatemenus=[dict(
132
+ type="buttons",
133
+ showactive=False,
134
+ buttons=[dict(label="Play",
135
+ method="animate",
136
+ args=[None, dict(frame=dict(duration=500, redraw=True), fromcurrent=True)])]
137
+ )],
138
+ sliders=[dict(
139
+ steps=[dict(label=str(i),
140
+ method="animate",
141
+ args=[[i], dict(frame=dict(duration=500, redraw=True), mode="immediate", transition=dict(duration=0))])
142
+ for i in range(len(steps))])
143
+ ]
144
+ )
145
+ st.plotly_chart(fig)
146
 
147
  # Streamlit App Interface
148
  def main():
 
183
  elif algorithm == "Selection Sort":
184
  steps = selection_sort(arr)
185
 
186
+ # Show the Plotly animation
187
+ plotly_animation(steps, algorithm)
188
 
189
  # Display the final sorted array
190
  st.write("Sorted Array:", arr)