File size: 2,108 Bytes
e1f5da3 |
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 79 80 81 82 83 |
import os
import pandas as pd
import streamlit as st
# Function to list files with .csv and .txt extensions in the current directory
def list_files():
icon_csv = "π "
icon_txt = "π "
current_directory = os.getcwd()
file_list = []
for filename in os.listdir(current_directory):
if filename.endswith(".csv"):
file_list.append(icon_csv + filename)
elif filename.endswith(".txt"):
file_list.append(icon_txt + filename)
return file_list
# Function to read a file
def read_file(file_path):
try:
df = pd.read_csv(file_path)
return df
except FileNotFoundError:
return "File not found."
# Function to delete a file
def delete_file(file_path):
try:
os.remove(file_path)
return f"{file_path} has been deleted."
except FileNotFoundError:
return "File not found."
# Function to write to a file
def write_file(file_path, data):
try:
data.to_csv(file_path, index=False)
return f"Successfully written to {file_path}."
except:
return "Error occurred while writing to file."
# Streamlit app
st.title("Question Set Import/Export Editor Downloader")
# File list
st.header("Files")
files = list_files()
for file in files:
st.write(file)
# File input and output
st.header("Edit Data")
file_path = st.text_input("Filename")
data = read_file(file_path)
if isinstance(data, pd.DataFrame):
st.dataframe(data)
st.write(f"Loaded {file_path}")
# Data editor
st.header("Edit Fields")
fields = ["ID", "Title", "Context", "Question", "AnswerText", "AnswerStart"]
new_data = {field: st.text_input(field) for field in fields}
# Add, delete, or save data
st.header("Actions")
add_data = st.button("Add Data")
delete_data = st.button("Delete Data")
save_data = st.button("Save Data")
if add_data:
data = data.append(new_data, ignore_index=True)
st.success("Data added.")
if delete_data:
data = data[data["ID"] != new_data["ID"]]
st.success("Data deleted.")
if save_data:
save_file(file_path, data)
st.success("Data saved.")
|