MonsterBoyTabs's picture
Update app.py
097d8e4 verified
import streamlit as st
from streamlit.components.v1 import html
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from scipy.signal import square, sawtooth
# Title of the app
st.title("Circuit Simulator with Waveform Analysis")
# Embed CircuitJS1
st.header("Build Your Circuit")
circuitjs_html = """
<div>
<iframe src="https://www.falstad.com/circuit/circuitjs.html"
width="100%"
height="600px"
style="border:none;">
</iframe>
</div>
"""
html(circuitjs_html, height=600)
# Add a description or instructions
st.markdown("""
### Instructions:
1. Use the CircuitJS1 simulator above to design and simulate electrical circuits.
2. You can add components, connect them, and simulate the circuit in real-time.
3. Explore the menu options in CircuitJS1 for advanced features.
""")
# Analysis and Waveform Section
st.header("Circuit Analysis and Waveform Visualization")
# Dummy analysis function (replace with actual analysis logic)
def analyze_circuit():
# Placeholder for analysis logic
# For now, we'll return dummy data
return pd.DataFrame({
"Total Voltage (V)": [5.0],
"Current (A)": [0.01],
"Resistance (Ω)": [500],
"Diode Drop (V)": [0.7]
})
# Waveform generation function
def generate_waveform(waveform_type):
t = np.linspace(0, 0.05, 1000) # Time vector
if waveform_type == "Sine":
wave = np.sin(2 * np.pi * 50 * t) # 50 Hz sine wave
elif waveform_type == "Square":
wave = square(2 * np.pi * 50 * t) # 50 Hz square wave
elif waveform_type == "Triangular":
wave = sawtooth(2 * np.pi * 50 * t) # 50 Hz triangular wave
else:
wave = np.zeros_like(t) # Default to zero
return t, wave
# Run Analysis Button
if st.button("▶️ Run Analysis"):
# Perform circuit analysis
results = analyze_circuit()
# Display analysis results
st.subheader("🔍 Analysis Results")
st.dataframe(results)
# Waveform visualization
st.subheader("📈 Waveform Visualization")
waveform_type = st.selectbox("Select Waveform Type", ["Sine", "Square", "Triangular"])
t, wave = generate_waveform(waveform_type)
# Plot waveform
fig = go.Figure()
fig.add_trace(go.Scatter(x=t, y=wave, mode="lines", name=waveform_type))
fig.update_layout(
title=f"{waveform_type} Waveform",
xaxis_title="Time (s)",
yaxis_title="Amplitude",
template="plotly_white"
)
st.plotly_chart(fig)
# Instructions for Analysis
with st.expander("❓ How to use"):
st.markdown("""
1. **Build Your Circuit**:
- Use the CircuitJS1 simulator above to design your circuit.
- Add components like resistors, capacitors, diodes, etc.
- Simulate the circuit to ensure it works as expected.
2. **Run Analysis**:
- Click the "Run Analysis" button to get the circuit's electrical properties.
- View the observation table for voltage, current, and resistance.
3. **Waveform Visualization**:
- Select a waveform type (Sine, Square, Triangular) to visualize.
- The waveform will be displayed based on the circuit's AC behavior.
""")