Spaces:
Sleeping
Sleeping
Create stream.py
Browse files
stream.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
|
| 5 |
+
# Title of the app
|
| 6 |
+
st.title("Mathematical Function Plotter")
|
| 7 |
+
|
| 8 |
+
# Input for the function
|
| 9 |
+
function = st.text_input("Enter a mathematical function (use 'x' as the variable):", "sin(x)")
|
| 10 |
+
|
| 11 |
+
# Input for the range
|
| 12 |
+
x_min = st.number_input("Enter the minimum value of x:", -10)
|
| 13 |
+
x_max = st.number_input("Enter the maximum value of x:", 10)
|
| 14 |
+
step = st.number_input("Enter the step size:", 0.1)
|
| 15 |
+
|
| 16 |
+
# Button to plot the function
|
| 17 |
+
if st.button("Plot"):
|
| 18 |
+
try:
|
| 19 |
+
# Define the x range
|
| 20 |
+
x = np.arange(x_min, x_max, step)
|
| 21 |
+
|
| 22 |
+
# Safely evaluate the function
|
| 23 |
+
y = eval(function, {"x": x, "np": np})
|
| 24 |
+
|
| 25 |
+
# Create the plot
|
| 26 |
+
plt.figure()
|
| 27 |
+
plt.plot(x, y, label=f'y = {function}')
|
| 28 |
+
plt.xlabel('x')
|
| 29 |
+
plt.ylabel('y')
|
| 30 |
+
plt.title('Plot of the function')
|
| 31 |
+
plt.legend()
|
| 32 |
+
plt.grid(True)
|
| 33 |
+
|
| 34 |
+
# Display the plot
|
| 35 |
+
st.pyplot(plt)
|
| 36 |
+
except Exception as e:
|
| 37 |
+
st.error(f"Error in plotting the function: {e}")
|
| 38 |
+
|
| 39 |
+
# Instructions for the user
|
| 40 |
+
st.sidebar.title("Instructions")
|
| 41 |
+
st.sidebar.info(
|
| 42 |
+
"""
|
| 43 |
+
Enter a mathematical function in the text input box using 'x' as the variable (e.g., 'sin(x)', 'cos(x)', 'x**2').
|
| 44 |
+
Specify the range for 'x' (minimum, maximum, and step size).
|
| 45 |
+
Click the "Plot" button to visualize the function.
|
| 46 |
+
"""
|
| 47 |
+
)
|