WNTR_QGIS / app.py
razaali10's picture
Update app.py
8a00073 verified
import streamlit as st
import wntr
import os
import pandas as pd
import plotly.express as px
from wntr_utils import run_simulation, plot_network_map
st.set_page_config(page_title="WNTR-QGIS Streamlit", layout="wide")
st.title("πŸ’§ WNTR-QGIS-style Water Network Analyzer")
# Upload file section
uploaded_file = st.file_uploader("Upload EPANET .inp File", type=["inp"])
@st.cache_resource
def load_wn_model(file_path):
return wntr.network.WaterNetworkModel(file_path)
if uploaded_file:
# Save uploaded file temporarily
with open("temp.inp", "wb") as f:
f.write(uploaded_file.read())
try:
with st.spinner("Loading water network model..."):
wn = load_wn_model("temp.inp")
st.success("Model loaded successfully!")
# User chooses simulation engine
sim_choice = st.radio("Choose Simulation Engine", ["EpanetSimulator", "WntrSimulator"])
with st.spinner("Running simulation..."):
results = run_simulation(wn, sim_choice)
st.success("Simulation completed!")
# Choose metric and time step
st.subheader("πŸ“Š Result Viewer")
metric = st.selectbox("Select Node Metric", ["pressure", "head"])
timestep = st.slider("Select Time Step", 0, len(results.node["pressure"].index)-1)
# Plot map
with st.spinner("Plotting network map..."):
fig = plot_network_map(wn, results, metric, timestep)
st.plotly_chart(fig, use_container_width=True)
except Exception as e:
st.error(f"❌ An error occurred: {e}")
else:
st.info("πŸ‘ˆ Please upload a valid EPANET `.inp` file to begin.")