forked from caelusweb3/MLCodeGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
97 lines (80 loc) · 2.71 KB
/
utils.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import streamlit as st
import pickle
import pandas as pd
import json
import base64
import uuid
import re
import jupytext
from bokeh.models.widgets import Div
import math
def notebook_header(text):
"""
Insert section header into a jinja file, formatted as notebook cell.
Leave 2 blank lines before the header.
"""
return f"""# # {text}
"""
def code_header(text):
"""
Insert section header into a jinja file, formatted as Python comment.
Leave 2 blank lines before the header.
"""
seperator_len = (75 - len(text)) / 2
seperator_len_left = math.floor(seperator_len)
seperator_len_right = math.ceil(seperator_len)
return f"# {'-' * seperator_len_left} {text} {'-' * seperator_len_right}"
def to_notebook(code):
"""Converts Python code to Jupyter notebook format."""
notebook = jupytext.reads(code, fmt="py")
return jupytext.writes(notebook, fmt="ipynb")
def open_link(url, new_tab=True):
if new_tab:
js = f"window.open('{url}')" # New tab or window
else:
js = f"window.location.href = '{url}'" # Current tab
html = '<img src onerror="{}">'.format(js)
div = Div(text=html)
st.bokeh_chart(div)
def download_button(
object_to_download, download_filename, button_text
):
try:
# some strings <-> bytes conversions necessary here
b64 = base64.b64encode(object_to_download.encode()).decode()
except AttributeError as e:
b64 = base64.b64encode(object_to_download).decode()
button_uuid = str(uuid.uuid4()).replace("-", "")
button_id = re.sub("\d+", "", button_uuid)
custom_css = f"""
<style>
#{button_id} {{
display: inline-flex;
align-items: center;
justify-content: center;
background-color: rgb(255, 255, 255);
color: rgb(38, 39, 48);
padding: .25rem .75rem;
position: relative;
text-decoration: none;
border-radius: 4px;
border-width: 1px;
border-style: solid;
border-color: rgb(230, 234, 241);
border-image: initial;
}}
#{button_id}:hover {{
border-color: rgb(246, 51, 102);
color: rgb(246, 51, 102);
}}
#{button_id}:active {{
box-shadow: none;
background-color: rgb(246, 51, 102);
color: white;
}}
</style> """
dl_link = (
custom_css
+ f'<a download="{download_filename}" id="{button_id}" href="data:file/txt;base64,{b64}">{button_text}</a><br><br>'
)
st.markdown(dl_link, unsafe_allow_html=True)