File size: 2,363 Bytes
1295ae5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import os
import pandas as pd

def read_aux_data():
    """

    Reads the file name and column names from separate files within the aux_data directory.



    Returns:

    column_names (str): The column names read from column_names.txt.

    file_name (str): The file name read from file_name.txt.



    """
    column_names_filename = 'aux_data/column_names.txt'
    file_name_filename = 'aux_data/file_name.txt'
    with open(column_names_filename, 'r') as column_file:
        column_names = [line.strip() for line in column_file.readlines()]

    # Read file name
    with open(file_name_filename, 'r') as file_file:
        file_name = file_file.read().strip()

    return column_names, file_name

def built_table():
    st.title("File Upload, Data Preview, and SQL Schema Creation")

    column_names, file_name = read_aux_data()

    st.write("## Load and Display Saved Column Data")
    st.write(column_names)

    st.write("## Load and Display Sample Data")
    file_path = os.path.join('data', file_name)

    if os.path.exists(f"{file_path}.csv"):
        df = pd.read_csv(f"{file_path}.csv")
        st.write(df.head(20))
    else:
        st.write("Sample data not found.")

    st.write("## Create SQL Schema")
    sql_schema = {}
    data_types = ['VARCHAR(250)', 'INT', 'FLOAT', 'DATE', 'DOUBLE']

    for col in column_names:
        selectbox_key = f"{col}_selectbox"
        data_type = st.selectbox(f"Select data type for column '{col}'", [''] + data_types, key=selectbox_key)
    
        if data_type == '':
            st.warning(f"Please select a data type for column '{col}'.")
            st.stop()  # Stop execution if a data type is not selected
        sql_schema[col] = data_type

    st.write("### Generated SQL Schema")
    create_table_query = f"CREATE TABLE {file_name} (\n"
    create_table_query += ",\n".join([f"    {col} {dtype}" for col, dtype in sql_schema.items()])
    create_table_query += "\n);"

    st.code(create_table_query, language='sql')

    if st.button("Schema is correct"):
        # Store the query in a file
        query_filename = 'aux_data/query.txt'
        with open(query_filename, 'w') as query_file:
            query_file.write(create_table_query)
        st.success("Query has been saved.")

    return None