I want to use this code to make a streamlit app to work with vectors, I already have the code to input the x and y values for 2 vectors, then I have the class vector2d whit methods for addition and substraction and a multiplication, then make a graphic. I repeat I already have the code i just need the form in stramlit
To upload files, please first save the app
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
class Vector2D:
def __init__(self, x, y):
self.x = x
self.y = y
def add(self, other):
return Vector2D(self.x + other.x, self.y + other.y)
def subtract(self, other):
return Vector2D(self.x - other.x, self.y - other.y)
def multiply(self, scalar):
return Vector2D(self.x * scalar, self.y * scalar)
def plot_vectors(v1, v2, result=None, operation=""):
fig, ax = plt.subplots(figsize=(8, 8))
# Set the plot limits
max_val = max(abs(v1.x), abs(v1.y), abs(v2.x), abs(v2.y))
limit = max_val + 2
ax.set_xlim(-limit, limit)
ax.set_ylim(-limit, limit)
# Add grid
ax.grid(True)
ax.axhline(y=0, color='k', linestyle='-', linewidth=0.5)
ax.axvline(x=0, color='k', linestyle='-', linewidth=0.5)
# Plot vectors
ax.quiver(0, 0, v1.x, v1.y, angles='xy', scale_units='xy', scale=1, color='b', label='Vector 1')
ax.quiver(0, 0, v2.x, v2.y, angles='xy', scale_units='xy', scale=1, color='r', label='Vector 2')
if result:
ax.quiver(0, 0, result.x, result.y, angles='xy', scale_units='xy', scale=1, color='g', label=f'Result ({operation})')
ax.legend()
ax.set_aspect('equal')
return fig
st.title("2D Vector Operations")
# Input form for vectors
with st.form("vector_input"):
st.subheader("Enter Vector Components")
col1, col2 = st.columns(2)
with col1:
st.write("Vector 1")
v1_x = st.number_input("X1", value=1.0, key="v1_x")
v1_y = st.number_input("Y1", value=1.0, key="v1_y")
with col2:
st.write("Vector 2")
v2_x = st.number_input("X2", value=2.0, key="v2_x")
v2_y = st.number_input("Y2", value=2.0, key="v2_y")
operation = st.selectbox(
"Select Operation",
["Addition", "Subtraction", "Multiply Vector 1", "Multiply Vector 2"]
)
if operation in ["Multiply Vector 1", "Multiply Vector 2"]:
scalar = st.number_input("Enter scalar value", value=2.0)
submitted = st.form_submit_button("Calculate")
if submitted:
v1 = Vector2D(v1_x, v1_y)
v2 = Vector2D(v2_x, v2_y)
if operation == "Addition":
result = v1.add(v2)
fig = plot_vectors(v1, v2, result, "Addition")
st.write(f"Result: ({result.x}, {result.y})")
elif operation == "Subtraction":
result = v1.subtract(v2)
fig = plot_vectors(v1, v2, result, "Subtraction")
st.write(f"Result: ({result.x}, {result.y})")
elif operation == "Multiply Vector 1":
result = v1.multiply(scalar)
fig = plot_vectors(v1, v2, result, f"V1 × {scalar}")
st.write(f"Result: ({result.x}, {result.y})")
elif operation == "Multiply Vector 2":
result = v2.multiply(scalar)
fig = plot_vectors(v1, v2, result, f"V2 × {scalar}")
st.write(f"Result: ({result.x}, {result.y})")
st.pyplot(fig)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?