File size: 2,600 Bytes
f447c88 01dd930 f447c88 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
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.")
|