awacke1 commited on
Commit
4e23c2d
·
1 Parent(s): 644b33f

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +76 -0
  2. download.py +139 -0
  3. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from st_aggrid import AgGrid
4
+ from st_aggrid.grid_options_builder import GridOptionsBuilder
5
+ from st_aggrid.shared import JsCode
6
+ from download import download_button
7
+ from st_aggrid import GridUpdateMode, DataReturnMode
8
+
9
+ # Page config is set once with icon title and display style. Wide mode since we want screen real estate for wide CSV files
10
+ st.set_page_config(page_icon="📝", page_title="📝CSV Data Analyzer📊", layout="wide")
11
+
12
+ # Style
13
+ def _max_width_():
14
+ max_width_str = f"max-width: 1800px;"
15
+ st.markdown(
16
+ f"""
17
+ <style>
18
+ .reportview-container .main .block-container{{
19
+ {max_width_str}
20
+ }}
21
+ </style>
22
+ """,
23
+ unsafe_allow_html=True,
24
+ )
25
+
26
+ # Title Bar with Images and Icons
27
+ col1, col2, col3 = st.columns([1,6,1])
28
+ with col1:
29
+ st.image("https://cdnb.artstation.com/p/assets/images/images/054/910/875/large/aaron-wacker-cyberpunk-computer-brain-design.jpg?1665656558",width=80,)
30
+ with col2:
31
+ st.title("📝 CSV Data Analyzer 📊")
32
+ with col3:
33
+ st.image("https://cdna.artstation.com/p/assets/images/images/054/910/878/large/aaron-wacker-cyberpunk-computer-devices-iot.jpg?1665656564",width=80,)
34
+
35
+ # Upload
36
+ c29, c30, c31 = st.columns([1, 6, 1])
37
+ with c30:
38
+ uploaded_file = st.file_uploader("", key="1", help="To activate 'wide mode', go to the menu > Settings > turn on 'wide mode'",)
39
+ if uploaded_file is not None:
40
+ file_container = st.expander("Check your uploaded .csv")
41
+ shows = pd.read_csv(uploaded_file)
42
+ uploaded_file.seek(0)
43
+ file_container.write(shows)
44
+ else:
45
+ st.info(f"""⬆️Upload a 📝.CSV file. Examples: [Chatbot](https://huggingface.co/datasets/awacke1/Carddata.csv) [Mindfulness](https://huggingface.co/datasets/awacke1/MindfulStory.csv) [Wikipedia](https://huggingface.co/datasets/awacke1/WikipediaSearch)""")
46
+ st.stop()
47
+
48
+ # DisplayGrid
49
+ gb = GridOptionsBuilder.from_dataframe(shows)
50
+ gb.configure_default_column(enablePivot=True, enableValue=True, enableRowGroup=True)
51
+ gb.configure_selection(selection_mode="multiple", use_checkbox=True)
52
+ gb.configure_side_bar()
53
+ gridOptions = gb.build()
54
+ st.success(f"""💡 Tip! Hold shift key when selecting rows to select multiple rows at once.""")
55
+ response = AgGrid(
56
+ shows,
57
+ gridOptions=gridOptions,
58
+ enable_enterprise_modules=True,
59
+ update_mode=GridUpdateMode.MODEL_CHANGED,
60
+ data_return_mode=DataReturnMode.FILTERED_AND_SORTED,
61
+ fit_columns_on_grid_load=False,
62
+ )
63
+
64
+ # Filters
65
+ df = pd.DataFrame(response["selected_rows"])
66
+ st.subheader("Filtered data will appear below 📊 ")
67
+ st.text("")
68
+ st.table(df)
69
+ st.text("")
70
+
71
+ # Download
72
+ c29, c30, c31 = st.columns([1, 1, 2])
73
+ with c29:
74
+ CSVButton = download_button(df,"Dataset.csv","Download CSV file",)
75
+ with c30:
76
+ CSVButton = download_button(df,"Dataset.txt","Download TXT file",)
download.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pickle
3
+ import pandas as pd
4
+ import json
5
+ import base64
6
+ import uuid
7
+ import re
8
+
9
+ import importlib.util
10
+
11
+
12
+ def import_from_file(module_name: str, filepath: str):
13
+ """
14
+ Imports a module from file.
15
+ Args:
16
+ module_name (str): Assigned to the module's __name__ parameter (does not
17
+ influence how the module is named outside of this function)
18
+ filepath (str): Path to the .py file
19
+ Returns:
20
+ The module
21
+ """
22
+ spec = importlib.util.spec_from_file_location(module_name, filepath)
23
+ module = importlib.util.module_from_spec(spec)
24
+ spec.loader.exec_module(module)
25
+ return module
26
+
27
+
28
+ def notebook_header(text):
29
+ """
30
+ Insert section header into a jinja file, formatted as notebook cell.
31
+ Leave 2 blank lines before the header.
32
+ """
33
+ return f"""# # {text}
34
+ """
35
+
36
+
37
+ def code_header(text):
38
+ """
39
+ Insert section header into a jinja file, formatted as Python comment.
40
+ Leave 2 blank lines before the header.
41
+ """
42
+ seperator_len = (75 - len(text)) / 2
43
+ seperator_len_left = math.floor(seperator_len)
44
+ seperator_len_right = math.ceil(seperator_len)
45
+ return f"# {'-' * seperator_len_left} {text} {'-' * seperator_len_right}"
46
+
47
+
48
+ def to_notebook(code):
49
+ """Converts Python code to Jupyter notebook format."""
50
+ notebook = jupytext.reads(code, fmt="py")
51
+ return jupytext.writes(notebook, fmt="ipynb")
52
+
53
+
54
+ def open_link(url, new_tab=True):
55
+ """Dirty hack to open a new web page with a streamlit button."""
56
+ # From: https://discuss.streamlit.io/t/how-to-link-a-button-to-a-webpage/1661/3
57
+ if new_tab:
58
+ js = f"window.open('{url}')" # New tab or window
59
+ else:
60
+ js = f"window.location.href = '{url}'" # Current tab
61
+ html = '<img src onerror="{}">'.format(js)
62
+ div = Div(text=html)
63
+ st.bokeh_chart(div)
64
+
65
+
66
+ def download_button(object_to_download, download_filename, button_text):
67
+ """
68
+ Generates a link to download the given object_to_download.
69
+ From: https://discuss.streamlit.io/t/a-download-button-with-custom-css/4220
70
+ Params:
71
+ ------
72
+ object_to_download: The object to be downloaded.
73
+ download_filename (str): filename and extension of file. e.g. mydata.csv,
74
+ some_txt_output.txt download_link_text (str): Text to display for download
75
+ link.
76
+ button_text (str): Text to display on download button (e.g. 'click here to download file')
77
+ pickle_it (bool): If True, pickle file.
78
+ Returns:
79
+ -------
80
+ (str): the anchor tag to download object_to_download
81
+ Examples:
82
+ --------
83
+ download_link(your_df, 'YOUR_DF.csv', 'Click to download data!')
84
+ download_link(your_str, 'YOUR_STRING.txt', 'Click to download text!')
85
+ """
86
+
87
+ # if:
88
+ if isinstance(object_to_download, bytes):
89
+ pass
90
+
91
+ elif isinstance(object_to_download, pd.DataFrame):
92
+ object_to_download = object_to_download.to_csv(index=False)
93
+ # Try JSON encode for everything else
94
+ else:
95
+ object_to_download = json.dumps(object_to_download)
96
+
97
+ try:
98
+ # some strings <-> bytes conversions necessary here
99
+ b64 = base64.b64encode(object_to_download.encode()).decode()
100
+ except AttributeError as e:
101
+ b64 = base64.b64encode(object_to_download).decode()
102
+
103
+ button_uuid = str(uuid.uuid4()).replace("-", "")
104
+ button_id = re.sub("\d+", "", button_uuid)
105
+
106
+ custom_css = f"""
107
+ <style>
108
+ #{button_id} {{
109
+ display: inline-flex;
110
+ align-items: center;
111
+ justify-content: center;
112
+ background-color: rgb(255, 255, 255);
113
+ color: rgb(38, 39, 48);
114
+ padding: .25rem .75rem;
115
+ position: relative;
116
+ text-decoration: none;
117
+ border-radius: 4px;
118
+ border-width: 1px;
119
+ border-style: solid;
120
+ border-color: rgb(230, 234, 241);
121
+ border-image: initial;
122
+ }}
123
+ #{button_id}:hover {{
124
+ border-color: rgb(246, 51, 102);
125
+ color: rgb(246, 51, 102);
126
+ }}
127
+ #{button_id}:active {{
128
+ box-shadow: none;
129
+ background-color: rgb(246, 51, 102);
130
+ color: white;
131
+ }}
132
+ </style> """
133
+
134
+ dl_link = (
135
+ custom_css
136
+ + f'<a download="{download_filename}" id="{button_id}" href="data:file/txt;base64,{b64}">{button_text}</a><br><br>'
137
+ )
138
+
139
+ st.markdown(dl_link, unsafe_allow_html=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ streamlit-aggrid==0.2.2-2
3
+ click==8