-
Notifications
You must be signed in to change notification settings - Fork 18
/
utils.py
80 lines (66 loc) · 2.57 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
import streamlit as st
import base64
from streamlit.components.v1 import html
from PATHS import NAVBAR_PATHS, SETTINGS
def inject_custom_css():
with open('assets/styles.css') as f:
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
def get_current_route():
try:
return st.query_params['nav']
except:
return None
def navbar_component():
with open("assets/images/settings.png", "rb") as image_file:
image_as_base64 = base64.b64encode(image_file.read())
navbar_items = ''
for key, value in NAVBAR_PATHS.items():
navbar_items += (f'<a class="navitem" href="/?nav={value}">{key}</a>')
settings_items = ''
for key, value in SETTINGS.items():
settings_items += (
f'<a href="/?nav={value}" class="settingsNav">{key}</a>')
component = rf'''
<nav class="container navbar" id="navbar">
<ul class="navlist">
{navbar_items}
</ul>
<div class="dropdown" id="settingsDropDown">
<img class="dropbtn" src="data:image/png;base64, {image_as_base64.decode("utf-8")}"/>
<div id="myDropdown" class="dropdown-content">
{settings_items}
</div>
</div>
</nav>
'''
st.markdown(component, unsafe_allow_html=True)
js = '''
<script>
// navbar elements
var navigationTabs = window.parent.document.getElementsByClassName("navitem");
var cleanNavbar = function(navigation_element) {
navigation_element.removeAttribute('target')
}
for (var i = 0; i < navigationTabs.length; i++) {
cleanNavbar(navigationTabs[i]);
}
// Dropdown hide / show
var dropdown = window.parent.document.getElementById("settingsDropDown");
dropdown.onclick = function() {
var dropWindow = window.parent.document.getElementById("myDropdown");
if (dropWindow.style.visibility == "hidden"){
dropWindow.style.visibility = "visible";
}else{
dropWindow.style.visibility = "hidden";
}
};
var settingsNavs = window.parent.document.getElementsByClassName("settingsNav");
var cleanSettings = function(navigation_element) {
navigation_element.removeAttribute('target')
}
for (var i = 0; i < settingsNavs.length; i++) {
cleanSettings(settingsNavs[i]);
}
</script>
'''
html(js)