Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
-
from
|
| 4 |
|
| 5 |
# Configuração do diretório de armazenamento
|
| 6 |
STORAGE_DIR = "storage"
|
|
@@ -11,44 +11,17 @@ if not os.path.exists(STORAGE_DIR):
|
|
| 11 |
|
| 12 |
# Função para listar arquivos em formato de árvore
|
| 13 |
def list_files_tree(directory):
|
| 14 |
-
file_tree =
|
| 15 |
for root, dirs, files in os.walk(directory):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
file_tree[folder_path] = {'folders': [], 'files': []}
|
| 19 |
-
for d in dirs:
|
| 20 |
-
file_tree[folder_path]['folders'].append(d)
|
| 21 |
-
for f in files:
|
| 22 |
-
file_tree[folder_path]['files'].append(f)
|
| 23 |
return file_tree
|
| 24 |
|
| 25 |
-
# Função para gerar árvore de arquivos
|
| 26 |
-
def generate_file_tree(directory):
|
| 27 |
-
tree_data = []
|
| 28 |
-
file_tree = list_files_tree(directory)
|
| 29 |
-
for folder, contents in file_tree.items():
|
| 30 |
-
path_parts = folder.split(os.sep)
|
| 31 |
-
current_level = tree_data
|
| 32 |
-
for part in path_parts:
|
| 33 |
-
found = False
|
| 34 |
-
for item in current_level:
|
| 35 |
-
if item['label'] == part:
|
| 36 |
-
current_level = item['children']
|
| 37 |
-
found = True
|
| 38 |
-
break
|
| 39 |
-
if not found:
|
| 40 |
-
new_item = {'label': part, 'children': []}
|
| 41 |
-
current_level.append(new_item)
|
| 42 |
-
current_level = new_item['children']
|
| 43 |
-
for f in contents['files']:
|
| 44 |
-
current_level.append({'label': f, 'children': []})
|
| 45 |
-
return tree_data
|
| 46 |
-
|
| 47 |
# Interface do Streamlit
|
| 48 |
st.title("Gerenciamento de Armazenamento em Nuvem")
|
| 49 |
|
| 50 |
# Upload de arquivo
|
| 51 |
-
uploaded_file = st.file_uploader("Escolha um arquivo para enviar", type=None, accept_multiple_files=False,
|
| 52 |
if uploaded_file is not None:
|
| 53 |
if uploaded_file.size > 200 * 1024 * 1024: # Limite de 200 MB
|
| 54 |
st.error("O arquivo excede o limite de 200 MB!")
|
|
@@ -60,15 +33,23 @@ if uploaded_file is not None:
|
|
| 60 |
|
| 61 |
# Exibir arquivos
|
| 62 |
st.header("Arquivos no Armazenamento")
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
# Deletar arquivo
|
| 74 |
st.header("Deletar Arquivo")
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
+
from st_aggrid import AgGrid, GridOptionsBuilder
|
| 4 |
|
| 5 |
# Configuração do diretório de armazenamento
|
| 6 |
STORAGE_DIR = "storage"
|
|
|
|
| 11 |
|
| 12 |
# Função para listar arquivos em formato de árvore
|
| 13 |
def list_files_tree(directory):
|
| 14 |
+
file_tree = []
|
| 15 |
for root, dirs, files in os.walk(directory):
|
| 16 |
+
for name in files:
|
| 17 |
+
file_tree.append({"path": os.path.relpath(os.path.join(root, name), directory), "name": name})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
return file_tree
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# Interface do Streamlit
|
| 21 |
st.title("Gerenciamento de Armazenamento em Nuvem")
|
| 22 |
|
| 23 |
# Upload de arquivo
|
| 24 |
+
uploaded_file = st.file_uploader("Escolha um arquivo para enviar", type=None, accept_multiple_files=False, help="Arraste e solte o arquivo aqui")
|
| 25 |
if uploaded_file is not None:
|
| 26 |
if uploaded_file.size > 200 * 1024 * 1024: # Limite de 200 MB
|
| 27 |
st.error("O arquivo excede o limite de 200 MB!")
|
|
|
|
| 33 |
|
| 34 |
# Exibir arquivos
|
| 35 |
st.header("Arquivos no Armazenamento")
|
| 36 |
+
files = list_files_tree(STORAGE_DIR)
|
| 37 |
+
if files:
|
| 38 |
+
df_files = [{"name": file["name"], "path": file["path"]} for file in files]
|
| 39 |
+
gb = GridOptionsBuilder.from_dataframe(df_files)
|
| 40 |
+
gb.configure_pagination()
|
| 41 |
+
gb.configure_side_bar()
|
| 42 |
+
gb.configure_selection('single')
|
| 43 |
+
grid_options = gb.build()
|
| 44 |
+
grid_response = AgGrid(df_files, gridOptions=grid_options, enable_enterprise_modules=True, fit_columns_on_grid_load=True)
|
| 45 |
+
|
| 46 |
+
selected = grid_response['selected_rows']
|
| 47 |
+
if selected:
|
| 48 |
+
selected_file_path = os.path.join(STORAGE_DIR, selected[0]['path'])
|
| 49 |
+
if selected_file_path.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.webp')):
|
| 50 |
+
st.image(selected_file_path, caption=selected_file_path, use_column_width=True)
|
| 51 |
+
else:
|
| 52 |
+
st.markdown(f"[{os.path.basename(selected_file_path)}]({selected_file_path})")
|
| 53 |
|
| 54 |
# Deletar arquivo
|
| 55 |
st.header("Deletar Arquivo")
|