King-Afridi commited on
Commit
3d26eb8
·
verified ·
1 Parent(s): b0f56f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -51
app.py CHANGED
@@ -1,68 +1,52 @@
1
  import streamlit as st
2
- import plotly.graph_objects as go
3
  import time
4
 
5
- # Function to create the traffic signal plot
6
- def plot_traffic_signal(signal_color):
7
- # Set up the colors for the signal
8
- colors = {'red': 'red', 'yellow': 'yellow', 'green': 'green'}
9
-
10
- # Create the figure
11
- fig = go.Figure()
12
 
13
- # Create the signal boxes
14
- fig.add_shape(
15
- type="rect",
16
- x0=0, y0=3, x1=1, y1=4,
17
- line=dict(color="black", width=2),
18
- fillcolor=colors['red'] if signal_color == 'red' else 'gray'
19
- )
20
- fig.add_shape(
21
- type="rect",
22
- x0=0, y0=2, x1=1, y1=3,
23
- line=dict(color="black", width=2),
24
- fillcolor=colors['yellow'] if signal_color == 'yellow' else 'gray'
25
- )
26
- fig.add_shape(
27
- type="rect",
28
- x0=0, y0=1, x1=1, y1=2,
29
- line=dict(color="black", width=2),
30
- fillcolor=colors['green'] if signal_color == 'green' else 'gray'
31
- )
32
-
33
- # Layout setup
34
- fig.update_layout(
35
- title="Animated Traffic Signal",
36
- xaxis=dict(showgrid=False, zeroline=False),
37
- yaxis=dict(showgrid=False, zeroline=False),
38
- width=200,
39
- height=400,
40
- showlegend=False,
41
- plot_bgcolor='white',
42
- margin=dict(l=0, r=0, t=50, b=50)
43
- )
44
 
45
- return fig
 
 
 
 
 
 
 
 
 
 
46
 
47
  # Streamlit UI
48
  st.title("Animated Traffic Signal")
49
 
50
- # Information
51
  st.markdown("""
52
- This Streamlit app shows an animated traffic signal that cycles through
53
- **red**, **yellow**, and **green** colors every few seconds.
54
  """)
55
 
56
- # Control the traffic light animation with a loop
57
- st.write("The traffic light will cycle every 3 seconds between red, yellow, and green.")
 
 
 
58
 
59
- # Run a loop that updates the signal color
60
  while True:
61
- for color in ['red', 'yellow', 'green']:
62
- # Plot the traffic signal with the current color
63
- fig = plot_traffic_signal(color)
64
- st.plotly_chart(fig, use_container_width=True)
 
 
65
 
66
- # Wait for 3 seconds before switching to the next color
67
  time.sleep(3)
68
 
 
1
  import streamlit as st
2
+ from PIL import Image, ImageDraw, ImageColor
3
  import time
4
 
5
+ # Function to generate traffic signal image based on the current color
6
+ def generate_traffic_signal(signal_color):
7
+ # Create a blank image with a white background
8
+ img = Image.new("RGB", (200, 400), "white")
9
+ draw = ImageDraw.Draw(img)
 
 
10
 
11
+ # Define the rectangle dimensions for the lights
12
+ light_width, light_height = 160, 80
13
+ margin = 20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ # Define the positions of the lights
16
+ positions = [(margin, margin), (margin, margin + light_height + margin), (margin, 2 * (light_height + margin))]
17
+ colors = ['red', 'yellow', 'green']
18
+
19
+ # Draw the lights based on the current signal color
20
+ for i, color in enumerate(colors):
21
+ # Set the fill color to the current light color if it's the active light
22
+ fill_color = signal_color if signal_color == color else "gray"
23
+ draw.rectangle([positions[i], (positions[i][0] + light_width, positions[i][1] + light_height)], fill=fill_color)
24
+
25
+ return img
26
 
27
  # Streamlit UI
28
  st.title("Animated Traffic Signal")
29
 
30
+ # Information text
31
  st.markdown("""
32
+ This Streamlit app simulates an animated traffic signal.
33
+ The signal will cycle through **Red**, **Yellow**, and **Green** every few seconds.
34
  """)
35
 
36
+ # Display the animation
37
+ signal_colors = ['red', 'yellow', 'green']
38
+
39
+ # Run the animation in a loop
40
+ st.write("The traffic light will cycle between red, yellow, and green every 3 seconds.")
41
 
 
42
  while True:
43
+ for color in signal_colors:
44
+ # Generate the traffic signal image
45
+ traffic_signal_img = generate_traffic_signal(color)
46
+
47
+ # Display the image using Streamlit
48
+ st.image(traffic_signal_img, caption=f"Traffic Signal: {color.capitalize()}", use_column_width=True)
49
 
50
+ # Wait for 3 seconds before changing to the next signal
51
  time.sleep(3)
52