amasood commited on
Commit
9d9c559
·
verified ·
1 Parent(s): 104bb52

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -37
app.py CHANGED
@@ -1,40 +1,21 @@
1
  import streamlit as st
2
  import numpy as np
3
  import plotly.graph_objects as go
 
4
 
5
- # Function to compute dot product
6
- def compute_dot_product(v1, v2):
7
- return np.dot(v1, v2)
8
-
9
- # Streamlit app
10
- st.title("Dot Product Visualization")
11
 
12
  # Initialize session state
13
  if "v1" not in st.session_state:
14
- st.session_state.v1 = np.array([1, 0])
15
  if "v2" not in st.session_state:
16
- st.session_state.v2 = np.array([0, 1])
17
-
18
- # Sidebar controls for vectors
19
- st.sidebar.header("Move Vectors")
20
-
21
- st.sidebar.subheader("Vector 1")
22
- x1 = st.sidebar.slider("X component", -5.0, 5.0, float(st.session_state.v1[0]), step=0.1)
23
- y1 = st.sidebar.slider("Y component", -5.0, 5.0, float(st.session_state.v1[1]), step=0.1)
24
-
25
- st.sidebar.subheader("Vector 2")
26
- x2 = st.sidebar.slider("X component ", -5.0, 5.0, float(st.session_state.v2[0]), step=0.1)
27
- y2 = st.sidebar.slider("Y component ", -5.0, 5.0, float(st.session_state.v2[1]), step=0.1)
28
-
29
- # Update vectors
30
- st.session_state.v1 = np.array([x1, y1])
31
- st.session_state.v2 = np.array([x2, y2])
32
 
33
- # Compute dot product
34
- dot_product = compute_dot_product(st.session_state.v1, st.session_state.v2)
35
-
36
- # Display dot product value
37
- st.markdown(f"### Dot Product: `{dot_product:.2f}`")
38
 
39
  # Create a plot
40
  fig = go.Figure()
@@ -44,8 +25,8 @@ fig.add_trace(go.Scatter(
44
  x=[0, st.session_state.v1[0]],
45
  y=[0, st.session_state.v1[1]],
46
  mode="lines+markers",
47
- marker=dict(size=8),
48
- line=dict(width=3),
49
  name="Vector 1"
50
  ))
51
 
@@ -54,18 +35,39 @@ fig.add_trace(go.Scatter(
54
  x=[0, st.session_state.v2[0]],
55
  y=[0, st.session_state.v2[1]],
56
  mode="lines+markers",
57
- marker=dict(size=8),
58
- line=dict(width=3),
59
  name="Vector 2"
60
  ))
61
 
62
  # Set axis properties
63
  fig.update_layout(
64
- xaxis=dict(range=[-6, 6], zeroline=True, title="X-axis"),
65
- yaxis=dict(range=[-6, 6], zeroline=True, title="Y-axis"),
66
- title="Vector Visualization",
 
67
  showlegend=True
68
  )
69
 
70
- # Display plot
71
- st.plotly_chart(fig)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import numpy as np
3
  import plotly.graph_objects as go
4
+ from streamlit_plotly_events import plotly_events
5
 
6
+ st.set_page_config(page_title="Dot Product Visualization", layout="wide")
 
 
 
 
 
7
 
8
  # Initialize session state
9
  if "v1" not in st.session_state:
10
+ st.session_state.v1 = np.array([3, 2]) # Initial position of vector 1
11
  if "v2" not in st.session_state:
12
+ st.session_state.v2 = np.array([-2, 3]) # Initial position of vector 2
13
+ if "selected_vector" not in st.session_state:
14
+ st.session_state.selected_vector = None # To track which vector is being moved
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ # Function to compute dot product
17
+ def compute_dot_product(v1, v2):
18
+ return np.dot(v1, v2)
 
 
19
 
20
  # Create a plot
21
  fig = go.Figure()
 
25
  x=[0, st.session_state.v1[0]],
26
  y=[0, st.session_state.v1[1]],
27
  mode="lines+markers",
28
+ marker=dict(size=12, color="blue"),
29
+ line=dict(width=4, color="blue"),
30
  name="Vector 1"
31
  ))
32
 
 
35
  x=[0, st.session_state.v2[0]],
36
  y=[0, st.session_state.v2[1]],
37
  mode="lines+markers",
38
+ marker=dict(size=12, color="red"),
39
+ line=dict(width=4, color="red"),
40
  name="Vector 2"
41
  ))
42
 
43
  # Set axis properties
44
  fig.update_layout(
45
+ xaxis=dict(range=[-5, 5], title="X-axis"),
46
+ yaxis=dict(range=[-5, 5], title="Y-axis"),
47
+ title="Interactive Dot Product Visualization",
48
+ dragmode="pan",
49
  showlegend=True
50
  )
51
 
52
+ # Display the interactive plot
53
+ st.subheader(f"Dot Product: `{compute_dot_product(st.session_state.v1, st.session_state.v2):.2f}`")
54
+ selected_points = plotly_events(fig, click_event=True)
55
+
56
+ # Check if a point is clicked
57
+ if selected_points:
58
+ clicked_x, clicked_y = selected_points[0]['x'], selected_points[0]['y']
59
+
60
+ # Determine which vector is clicked
61
+ if np.allclose([clicked_x, clicked_y], st.session_state.v1, atol=0.5):
62
+ st.session_state.selected_vector = "v1"
63
+ elif np.allclose([clicked_x, clicked_y], st.session_state.v2, atol=0.5):
64
+ st.session_state.selected_vector = "v2"
65
+
66
+ # Move the selected vector using sliders
67
+ if st.session_state.selected_vector:
68
+ st.sidebar.subheader(f"Move {st.session_state.selected_vector}")
69
+ new_x = st.sidebar.slider("X component", -5.0, 5.0, float(st.session_state[st.session_state.selected_vector][0]), step=0.1)
70
+ new_y = st.sidebar.slider("Y component", -5.0, 5.0, float(st.session_state[st.session_state.selected_vector][1]), step=0.1)
71
+
72
+ # Update vector position
73
+ st.session_state[st.session_state.selected_vector] = np.array([new_x, new_y])