File size: 1,418 Bytes
b80a552
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import sqlite3
from io import StringIO

# Constants
DB_PATH = "db/database.db"

# Fetch all datasets from SQLite
def get_datasets():
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    cursor.execute("SELECT id, name FROM datasets")
    datasets = cursor.fetchall()
    conn.close()
    return datasets

# Load dataset content by ID
def load_dataset_content(dataset_id):
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    cursor.execute("SELECT content FROM datasets WHERE id = ?", (dataset_id,))
    content = cursor.fetchone()
    conn.close()
    if content:
        return content[0]
    return None

# Streamlit app for downloading files
st.title("Download Datasets from Database")
st.write("Below is the list of all datasets available in the database. Select and download any file.")

datasets = get_datasets()

if datasets:
    for dataset_id, dataset_name in datasets:
        st.write(f"**Dataset ID**: {dataset_id} | **Name**: {dataset_name}")
        dataset_content = load_dataset_content(dataset_id)
        
        if dataset_content:
            st.download_button(
                label=f"Download {dataset_name}",
                data=dataset_content,
                file_name=dataset_name,
                mime="text/csv"
            )
else:
    st.write("No datasets available in the database.")