justheuristic commited on
Commit
59f1bbf
·
1 Parent(s): 1eae737
Files changed (1) hide show
  1. st_helpers.py +15 -0
st_helpers.py CHANGED
@@ -1,5 +1,8 @@
 
 
1
  import streamlit as st
2
  import streamlit.components.v1 as components
 
3
 
4
  with open("static/header.html", 'r', encoding='utf-8') as f:
5
  header_html = f.read()
@@ -13,6 +16,18 @@ with open("static/meta.html", 'r', encoding='utf-8') as f:
13
  meta_html = f.read()
14
 
15
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def make_header():
17
  components.html(f"<style>{header_style_css}</style>{header_html}<script>{header_animate_js}</script>", height=260)
18
  st.markdown(meta_html, unsafe_allow_html=True)
 
1
+ import pathlib
2
+
3
  import streamlit as st
4
  import streamlit.components.v1 as components
5
+ from bs4 import BeautifulSoup
6
 
7
  with open("static/header.html", 'r', encoding='utf-8') as f:
8
  header_html = f.read()
 
16
  meta_html = f.read()
17
 
18
 
19
+ GA_JS = """alert("Hello world!")"""
20
+
21
+ # Insert the script in the head tag of the static template inside your virtual environement
22
+ index_path = pathlib.Path(st.__file__).parent / "static" / "index.html"
23
+ soup = BeautifulSoup(index_path.read_text(), features="lxml")
24
+ if not soup.find(id='custom-js'):
25
+ script_tag = soup.new_tag("script", id='custom-js')
26
+ script_tag.string = GA_JS
27
+ soup.head.append(script_tag)
28
+ index_path.write_text(str(soup))
29
+
30
+
31
  def make_header():
32
  components.html(f"<style>{header_style_css}</style>{header_html}<script>{header_animate_js}</script>", height=260)
33
  st.markdown(meta_html, unsafe_allow_html=True)