Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Define the data folder path
|
| 6 |
+
DATA_FOLDER = "data"
|
| 7 |
+
|
| 8 |
+
# Ensure the data folder exists
|
| 9 |
+
if not os.path.exists(DATA_FOLDER):
|
| 10 |
+
os.makedirs(DATA_FOLDER)
|
| 11 |
+
|
| 12 |
+
st.title("JSON File Uploader")
|
| 13 |
+
|
| 14 |
+
uploaded_file = st.file_uploader("Upload a JSON file", type=["json"])
|
| 15 |
+
|
| 16 |
+
if uploaded_file is not None:
|
| 17 |
+
try:
|
| 18 |
+
# Read the JSON file to validate its content (optional)
|
| 19 |
+
json.load(uploaded_file)
|
| 20 |
+
uploaded_file.seek(0) # Reset file pointer after reading
|
| 21 |
+
|
| 22 |
+
# Get the filename
|
| 23 |
+
file_name = uploaded_file.name
|
| 24 |
+
|
| 25 |
+
# Construct the full file path
|
| 26 |
+
file_path = os.path.join(DATA_FOLDER, file_name)
|
| 27 |
+
|
| 28 |
+
# Save the file to the data folder
|
| 29 |
+
with open(file_path, "wb") as f:
|
| 30 |
+
f.write(uploaded_file.read())
|
| 31 |
+
|
| 32 |
+
st.success(f"File '{file_name}' successfully uploaded and saved to:")
|
| 33 |
+
st.code(file_path, language="plaintext")
|
| 34 |
+
|
| 35 |
+
except json.JSONDecodeError:
|
| 36 |
+
st.error("Error: The uploaded file is not a valid JSON file.")
|
| 37 |
+
except Exception as e:
|
| 38 |
+
st.error(f"An error occurred: {e}")
|
| 39 |
+
else:
|
| 40 |
+
st.info("Please upload a JSON file.")
|