|
| 1 | +""" |
| 2 | +Copied from: https://github.com/jrieke/traingenerator |
| 3 | +
|
| 4 | +Update index.html from streamlit by |
| 5 | +- adding tracking code for Google Analytics |
| 6 | +- adding meta tags for search engines |
| 7 | +- adding meta tags for social preview |
| 8 | +WARNING: This changes your existing streamlit installation (specifically the file |
| 9 | +static/index.html in streamlit's main folder). It should only be called once after |
| 10 | +installation, so this file doesn't get cluttered! |
| 11 | +The tag from Google Analytics (G-XXXXXXXXXX) has to be stored in an environment variable |
| 12 | +GOOGLE_ANALYTICS_TAG (or in a .env file). |
| 13 | +""" |
| 14 | + |
| 15 | +import os |
| 16 | +import sys |
| 17 | + |
| 18 | +import streamlit as st |
| 19 | + |
| 20 | + |
| 21 | +def replace_in_file(filename, oldvalue, newvalue): |
| 22 | + """Replace string in a file and optionally create backup_filename.""" |
| 23 | + # Read in the file |
| 24 | + with open(filename, "r") as f: |
| 25 | + filedata = f.read() |
| 26 | + |
| 27 | + # Replace the target string |
| 28 | + filedata = filedata.replace(oldvalue, newvalue) |
| 29 | + |
| 30 | + # Write the file out again |
| 31 | + with open(filename, "w") as f: |
| 32 | + f.write(filedata) |
| 33 | + |
| 34 | + |
| 35 | +# Find path to streamlit's index.html. |
| 36 | +st_dir = os.path.dirname(st.__file__) |
| 37 | +index_filename = os.path.join(st_dir, "static", "index.html") |
| 38 | + |
| 39 | +# Insert tracking code for Google Analytics. |
| 40 | +tag = os.getenv("GOOGLE_ANALYTICS_TAG") |
| 41 | +if not tag: |
| 42 | + print("No tag provided, analytics is deactivated") |
| 43 | + sys.exit(1) |
| 44 | + |
| 45 | +tracking_code = f"""<!-- Global site tag (gtag.js) - Google Analytics --><script async src="https://www.googletagmanager.com/gtag/js?id={tag}"></script><script>window.dataLayer = window.dataLayer || []; function gtag(){{dataLayer.push(arguments);}} gtag('js', new Date()); gtag('config', '{tag}');</script>""" |
| 46 | + |
| 47 | +clarity_tag = os.getenv("CLARITY_TAG") |
| 48 | +if clarity_tag: |
| 49 | + # Add clarity tracking code |
| 50 | + clarity_tracking_code = f""" |
| 51 | + <script type="text/javascript"> |
| 52 | + (function(c,l,a,r,i,t,y){{ |
| 53 | + c[a]=c[a]||function(){{(c[a].q=c[a].q||[]).push(arguments)}}; |
| 54 | + t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i; |
| 55 | + y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y); |
| 56 | + }})(window, document, "clarity", "script", "{clarity_tag}"); |
| 57 | + </script> |
| 58 | + """ |
| 59 | + |
| 60 | + tracking_code += clarity_tracking_code |
| 61 | + |
| 62 | +size_before = os.stat(index_filename).st_size |
| 63 | +replace_in_file(index_filename, "<head>", "<head>" + tracking_code) |
| 64 | +size_after = os.stat(index_filename).st_size |
| 65 | + |
| 66 | +print("Inserted tracking code into:", index_filename) |
| 67 | +print("Size before:", size_before) |
| 68 | +print("Size after: ", size_after) |
| 69 | + |
| 70 | +# Insert meta tags for search & social preview. |
| 71 | +# Older info but good summary: https://css-tricks.com/essential-meta-tags-social-media/ |
| 72 | +# 2020 info: https://stackoverflow.com/questions/19778620/provide-an-image-for-whatsapp-link-sharing |
| 73 | +META_TAGS = """ |
| 74 | +<!-- Meta tags for search engines --> |
| 75 | +<meta name="description" content="Python functions with superpowers. Instantly deploy simple functions with REST API, UI, and more."> |
| 76 | +<!-- Meta tags for social preview --> |
| 77 | +<meta property="og:title" content="Opyrator Playground"> |
| 78 | +<meta property="og:description" content="Python functions with superpowers"> |
| 79 | +<meta property="og:url" content="https://github.com/ml-tooling/opyrator"> |
| 80 | +<meta property="og:site_name" content="Opyrator Playground"> |
| 81 | +<meta name="twitter:image:alt" content="Opyrator Playground"> |
| 82 | +""" |
| 83 | + |
| 84 | +# <meta name="twitter:card" content="summary_large_image"> |
| 85 | +# <meta property="og:image" content="https://github.com/jrieke/traingenerator/raw/main/docs/assets/social-preview-tiny.png"> |
| 86 | + |
| 87 | +size_before = os.stat(index_filename).st_size |
| 88 | +replace_in_file(index_filename, "<head>", "<head>" + META_TAGS) |
| 89 | +size_after = os.stat(index_filename).st_size |
| 90 | + |
| 91 | +print("Inserted meta tags into:", index_filename) |
| 92 | +print("Size before:", size_before) |
| 93 | +print("Size after: ", size_after) |
0 commit comments