File size: 1,847 Bytes
c4b27ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import datetime

import create_object as co

import duckdb
df = pd.read_csv("data/online_retail.csv")

# データ型整備:ID項目のSTRING化など
df = df.astype({'CustomerID': 'object'})
df = df[df["UnitPrice"] * df["Quantity"] > 0]

country_list = df["Country"].unique()

st.set_page_config(
    page_title="購買データ分析App", 
    layout="wide", 
)

def main():
    st.title("購買データ分析App")

    with st.sidebar.form(key="my_form"):
        analysis_menu = st.selectbox("分析メニュー", co.analysis_menu_list)

        country = st.multiselect("国を選択してください。", country_list)
        if len(country) != 0:
            country = "','".join(country)
            country = f"Country in ('{country}')"
        else:
            country = "True"

        st.write("2010年の日付を入れてください。")
        start_date = st.date_input("開始日", datetime.date(2010, 1, 1))
        end_date = st.date_input("終了日", datetime.date(2010, 12, 31))

        submit_button = st.form_submit_button(label = "分析開始")

        
    if submit_button:
         # 処理を実行
        sql = co.create_sql(analysis_menu, country, start_date, end_date)
        df_output = duckdb.query(sql).to_df()

        try:
            fig = co.create_graph(analysis_menu, df_output)
            st.plotly_chart(fig)
        except:
            print("グラフ無し")

        st.table(df_output.head(100))
        st.write("上位100行まで、全体を見たい場合はCSVでダウンロードしてください。")

        st.download_button(
        "Press to Download",
        df_output.to_csv(index=False).encode('utf-8'),
        "file.csv",
        "text/csv",
        key='download-csv'
        )
    
if __name__ == '__main__':
    main()