db_query / apps /import_physical_db.py
DavMelchi's picture
add site_band_config
01dd930
raw
history blame
2.6 kB
import os
import pandas as pd
import streamlit as st
# Define the folder path where the physical database will be stored
physical_db_folder = "physical_db"
physical_db_file = os.path.join(physical_db_folder, "physical_database.csv")
# Ensure the folder exists
if not os.path.exists(physical_db_folder):
os.makedirs(physical_db_folder)
# Streamlit app title
st.title("Physical Database Management")
# Step 1: Upload a new CSV file
uploaded_file = st.file_uploader(
"Upload a CSV file of new physical database", type="csv"
)
if uploaded_file is not None:
# Step 2: Check if the file contains the required columns
df = pd.read_csv(uploaded_file)
required_columns = [
"CODE",
"sector",
"Code_Sector",
"Azimut",
"Longitude",
"Latitude",
"Hauteur",
]
if not all(col in df.columns for col in required_columns):
st.error(
f"Error: The file must contain the following columns: {', '.join(required_columns)}"
)
elif len(df) <= 500:
st.error("Error: The file must contain more than 500 rows.")
else:
st.success(
"File successfully validated. Click on the Save button to save the new physical database file."
)
# Display the DataFrame
st.write(df)
# Step 6: Add button to save the new file
if st.button("Save the new physical database file", type="primary"):
# Step 4: Remove everything in the folder physical_db/
for file in os.listdir(physical_db_folder):
os.remove(os.path.join(physical_db_folder, file))
# Step 5: Rename and save the new file
df.to_csv(physical_db_file, index=False)
st.success(f"New physical database saved Successfully.")
# # Step 7: Add button to download the existing physical_database.csv file
# if os.path.exists(physical_db_file):
# st.subheader("Download the existing physical_database.csv file")
# with open(physical_db_file, "rb") as f:
# st.download_button(
# label="Download physical_database.csv",
# data=f,
# file_name="physical_database.csv",
# )
# Step 8: Add button to show the existing physical_database.csv file
if os.path.exists(physical_db_file):
st.subheader("Show the existing physical_database file")
if st.button("Show actual physical database"):
df_existing = pd.read_csv(physical_db_file)
st.dataframe(df_existing)
else:
st.warning("No physical_database.csv file found in the physical_db/ folder.")