Spaces:
Runtime error
Runtime error
not usable in project
Browse files- functionforDownloadButtons.py +0 -171
functionforDownloadButtons.py
DELETED
@@ -1,171 +0,0 @@
|
|
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 |
-
|
16 |
-
Args:
|
17 |
-
module_name (str): Assigned to the module's __name__ parameter (does not
|
18 |
-
influence how the module is named outside of this function)
|
19 |
-
filepath (str): Path to the .py file
|
20 |
-
|
21 |
-
Returns:
|
22 |
-
The module
|
23 |
-
"""
|
24 |
-
spec = importlib.util.spec_from_file_location(module_name, filepath)
|
25 |
-
module = importlib.util.module_from_spec(spec)
|
26 |
-
spec.loader.exec_module(module)
|
27 |
-
return module
|
28 |
-
|
29 |
-
|
30 |
-
def notebook_header(text):
|
31 |
-
"""
|
32 |
-
Insert section header into a jinja file, formatted as notebook cell.
|
33 |
-
|
34 |
-
Leave 2 blank lines before the header.
|
35 |
-
"""
|
36 |
-
return f"""# # {text}
|
37 |
-
|
38 |
-
"""
|
39 |
-
|
40 |
-
|
41 |
-
def code_header(text):
|
42 |
-
"""
|
43 |
-
Insert section header into a jinja file, formatted as Python comment.
|
44 |
-
|
45 |
-
Leave 2 blank lines before the header.
|
46 |
-
"""
|
47 |
-
seperator_len = (75 - len(text)) / 2
|
48 |
-
seperator_len_left = math.floor(seperator_len)
|
49 |
-
seperator_len_right = math.ceil(seperator_len)
|
50 |
-
return f"# {'-' * seperator_len_left} {text} {'-' * seperator_len_right}"
|
51 |
-
|
52 |
-
|
53 |
-
def to_notebook(code):
|
54 |
-
"""Converts Python code to Jupyter notebook format."""
|
55 |
-
notebook = jupytext.reads(code, fmt="py")
|
56 |
-
return jupytext.writes(notebook, fmt="ipynb")
|
57 |
-
|
58 |
-
|
59 |
-
def open_link(url, new_tab=True):
|
60 |
-
"""Dirty hack to open a new web page with a streamlit button."""
|
61 |
-
# From: https://discuss.streamlit.io/t/how-to-link-a-button-to-a-webpage/1661/3
|
62 |
-
if new_tab:
|
63 |
-
js = f"window.open('{url}')" # New tab or window
|
64 |
-
else:
|
65 |
-
js = f"window.location.href = '{url}'" # Current tab
|
66 |
-
html = '<img src onerror="{}">'.format(js)
|
67 |
-
div = Div(text=html)
|
68 |
-
st.bokeh_chart(div)
|
69 |
-
|
70 |
-
|
71 |
-
def download_button(object_to_download, download_filename, button_text):
|
72 |
-
"""
|
73 |
-
Generates a link to download the given object_to_download.
|
74 |
-
|
75 |
-
From: https://discuss.streamlit.io/t/a-download-button-with-custom-css/4220
|
76 |
-
|
77 |
-
Params:
|
78 |
-
------
|
79 |
-
object_to_download: The object to be downloaded.
|
80 |
-
download_filename (str): filename and extension of file. e.g. mydata.csv,
|
81 |
-
some_txt_output.txt download_link_text (str): Text to display for download
|
82 |
-
link.
|
83 |
-
|
84 |
-
button_text (str): Text to display on download button (e.g. 'click here to download file')
|
85 |
-
pickle_it (bool): If True, pickle file.
|
86 |
-
|
87 |
-
Returns:
|
88 |
-
-------
|
89 |
-
(str): the anchor tag to download object_to_download
|
90 |
-
|
91 |
-
Examples:
|
92 |
-
--------
|
93 |
-
download_link(your_df, 'YOUR_DF.csv', 'Click to download data!')
|
94 |
-
download_link(your_str, 'YOUR_STRING.txt', 'Click to download text!')
|
95 |
-
|
96 |
-
"""
|
97 |
-
# if pickle_it:
|
98 |
-
# try:
|
99 |
-
# object_to_download = pickle.dumps(object_to_download)
|
100 |
-
# except pickle.PicklingError as e:
|
101 |
-
# st.write(e)
|
102 |
-
# return None
|
103 |
-
|
104 |
-
# if:
|
105 |
-
if isinstance(object_to_download, bytes):
|
106 |
-
pass
|
107 |
-
|
108 |
-
elif isinstance(object_to_download, pd.DataFrame):
|
109 |
-
object_to_download = object_to_download.to_csv(index=False)
|
110 |
-
# Try JSON encode for everything else
|
111 |
-
else:
|
112 |
-
object_to_download = json.dumps(object_to_download)
|
113 |
-
|
114 |
-
try:
|
115 |
-
# some strings <-> bytes conversions necessary here
|
116 |
-
b64 = base64.b64encode(object_to_download.encode()).decode()
|
117 |
-
except AttributeError as e:
|
118 |
-
b64 = base64.b64encode(object_to_download).decode()
|
119 |
-
|
120 |
-
button_uuid = str(uuid.uuid4()).replace("-", "")
|
121 |
-
button_id = re.sub("\d+", "", button_uuid)
|
122 |
-
|
123 |
-
custom_css = f"""
|
124 |
-
<style>
|
125 |
-
#{button_id} {{
|
126 |
-
display: inline-flex;
|
127 |
-
align-items: center;
|
128 |
-
justify-content: center;
|
129 |
-
background-color: rgb(255, 255, 255);
|
130 |
-
color: rgb(38, 39, 48);
|
131 |
-
padding: .25rem .75rem;
|
132 |
-
position: relative;
|
133 |
-
text-decoration: none;
|
134 |
-
border-radius: 4px;
|
135 |
-
border-width: 1px;
|
136 |
-
border-style: solid;
|
137 |
-
border-color: rgb(230, 234, 241);
|
138 |
-
border-image: initial;
|
139 |
-
}}
|
140 |
-
#{button_id}:hover {{
|
141 |
-
border-color: rgb(246, 51, 102);
|
142 |
-
color: rgb(246, 51, 102);
|
143 |
-
}}
|
144 |
-
#{button_id}:active {{
|
145 |
-
box-shadow: none;
|
146 |
-
background-color: rgb(246, 51, 102);
|
147 |
-
color: white;
|
148 |
-
}}
|
149 |
-
</style> """
|
150 |
-
|
151 |
-
dl_link = (
|
152 |
-
custom_css
|
153 |
-
+ f'<a download="{download_filename}" id="{button_id}" href="data:file/txt;base64,{b64}">{button_text}</a><br><br>'
|
154 |
-
)
|
155 |
-
# dl_link = f'<a download="{download_filename}" id="{button_id}" href="data:file/txt;base64,{b64}"><input type="button" kind="primary" value="{button_text}"></a><br></br>'
|
156 |
-
|
157 |
-
st.markdown(dl_link, unsafe_allow_html=True)
|
158 |
-
|
159 |
-
|
160 |
-
# def download_link(
|
161 |
-
# content, label="Download", filename="file.txt", mimetype="text/plain"
|
162 |
-
# ):
|
163 |
-
# """Create a HTML link to download a string as a file."""
|
164 |
-
# # From: https://discuss.streamlit.io/t/how-to-download-file-in-streamlit/1806/9
|
165 |
-
# b64 = base64.b64encode(
|
166 |
-
# content.encode()
|
167 |
-
# ).decode() # some strings <-> bytes conversions necessary here
|
168 |
-
# href = (
|
169 |
-
# f'<a href="data:{mimetype};base64,{b64}" download="{filename}">{label}</a>'
|
170 |
-
# )
|
171 |
-
# return href
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|