awacke1 commited on
Commit
45e5a75
·
1 Parent(s): 384f9d8

Create new file

Browse files
Files changed (1) hide show
  1. app.py +137 -0
app.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+
4
+ ###################################
5
+ from st_aggrid import AgGrid
6
+ from st_aggrid.grid_options_builder import GridOptionsBuilder
7
+ from st_aggrid.shared import JsCode
8
+
9
+ ###################################
10
+
11
+ from functionforDownloadButtons import download_button
12
+
13
+ ###################################
14
+
15
+
16
+ def _max_width_():
17
+ max_width_str = f"max-width: 1800px;"
18
+ st.markdown(
19
+ f"""
20
+ <style>
21
+ .reportview-container .main .block-container{{
22
+ {max_width_str}
23
+ }}
24
+ </style>
25
+ """,
26
+ unsafe_allow_html=True,
27
+ )
28
+
29
+ st.set_page_config(page_icon="✂️", page_title="CSV Wrangler")
30
+
31
+ # st.image("https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/240/apple/285/balloon_1f388.png", width=100)
32
+ st.image(
33
+ "https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/240/apple/285/scissors_2702-fe0f.png",
34
+ width=100,
35
+ )
36
+
37
+ st.title("CSV Wrangler")
38
+
39
+ # st.caption(
40
+ # "PRD : TBC | Streamlit Ag-Grid from Pablo Fonseca: https://pypi.org/project/streamlit-aggrid/"
41
+ # )
42
+
43
+
44
+ # ModelType = st.radio(
45
+ # "Choose your model",
46
+ # ["Flair", "DistilBERT (Default)"],
47
+ # help="At present, you can choose between 2 models (Flair or DistilBERT) to embed your text. More to come!",
48
+ # )
49
+
50
+ # with st.expander("ToDo's", expanded=False):
51
+ # st.markdown(
52
+ # """
53
+ # - Add pandas.json_normalize() - https://streamlit.slack.com/archives/D02CQ5Z5GHG/p1633102204005500
54
+ # - **Remove 200 MB limit and test with larger CSVs**. Currently, the content is embedded in base64 format, so we may end up with a large HTML file for the browser to render
55
+ # - **Add an encoding selector** (to cater for a wider array of encoding types)
56
+ # - **Expand accepted file types** (currently only .csv can be imported. Could expand to .xlsx, .txt & more)
57
+ # - Add the ability to convert to pivot → filter → export wrangled output (Pablo is due to change AgGrid to allow export of pivoted/grouped data)
58
+ # """
59
+ # )
60
+ #
61
+ # st.text("")
62
+
63
+
64
+ c29, c30, c31 = st.columns([1, 6, 1])
65
+
66
+ with c30:
67
+
68
+ uploaded_file = st.file_uploader(
69
+ "",
70
+ key="1",
71
+ help="To activate 'wide mode', go to the hamburger menu > Settings > turn on 'wide mode'",
72
+ )
73
+
74
+ if uploaded_file is not None:
75
+ file_container = st.expander("Check your uploaded .csv")
76
+ shows = pd.read_csv(uploaded_file)
77
+ uploaded_file.seek(0)
78
+ file_container.write(shows)
79
+
80
+ else:
81
+ st.info(
82
+ f"""
83
+ 👆 Upload a .csv file first. Sample to try: [biostats.csv](https://people.sc.fsu.edu/~jburkardt/data/csv/biostats.csv)
84
+ """
85
+ )
86
+
87
+ st.stop()
88
+
89
+ from st_aggrid import GridUpdateMode, DataReturnMode
90
+
91
+ gb = GridOptionsBuilder.from_dataframe(shows)
92
+ # enables pivoting on all columns, however i'd need to change ag grid to allow export of pivoted/grouped data, however it select/filters groups
93
+ gb.configure_default_column(enablePivot=True, enableValue=True, enableRowGroup=True)
94
+ gb.configure_selection(selection_mode="multiple", use_checkbox=True)
95
+ gb.configure_side_bar() # side_bar is clearly a typo :) should by sidebar
96
+ gridOptions = gb.build()
97
+
98
+ st.success(
99
+ f"""
100
+ 💡 Tip! Hold the shift key when selecting rows to select multiple rows at once!
101
+ """
102
+ )
103
+
104
+ response = AgGrid(
105
+ shows,
106
+ gridOptions=gridOptions,
107
+ enable_enterprise_modules=True,
108
+ update_mode=GridUpdateMode.MODEL_CHANGED,
109
+ data_return_mode=DataReturnMode.FILTERED_AND_SORTED,
110
+ fit_columns_on_grid_load=False,
111
+ )
112
+
113
+ df = pd.DataFrame(response["selected_rows"])
114
+
115
+ st.subheader("Filtered data will appear below 👇 ")
116
+ st.text("")
117
+
118
+ st.table(df)
119
+
120
+ st.text("")
121
+
122
+ c29, c30, c31 = st.columns([1, 1, 2])
123
+
124
+ with c29:
125
+
126
+ CSVButton = download_button(
127
+ df,
128
+ "File.csv",
129
+ "Download to CSV",
130
+ )
131
+
132
+ with c30:
133
+ CSVButton = download_button(
134
+ df,
135
+ "File.csv",
136
+ "Download to TXT",
137
+ )