最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - can I display custom javascript in streamlit web app - Stack Overflow

programmeradmin2浏览0评论

I am working on a parking data app using Streamlit library in python 3.7, I want to display the availability of parking spots using custom JavaScript for visualization. Is it possible to display HTML/javascript elements in streamlit web app

I am working on a parking data app using Streamlit library in python 3.7, I want to display the availability of parking spots using custom JavaScript for visualization. Is it possible to display HTML/javascript elements in streamlit web app

Share Improve this question edited Jun 15, 2021 at 0:15 Reda Elmar asked Jun 14, 2021 at 21:25 Reda ElmarReda Elmar 1231 gold badge1 silver badge7 bronze badges 4
  • good luck. Come back when you get error message. – furas Commented Jun 15, 2021 at 0:08
  • @furas I am asking it is possible, I don't seem to find anything about it in Streamlit documentation. – Reda Elmar Commented Jun 15, 2021 at 0:18
  • then check source code. – furas Commented Jun 15, 2021 at 0:20
  • using Google streamlit add htmlI found How to render already prepared html code with Streamlit? which uses st.markdown(html_string, unsafe_allow_html=True). It think you could use it to add HTML and JavaScript. But in documentation I found Create a Streamlit Component and maybe this can be more interesting. – furas Commented Jun 15, 2021 at 0:32
Add a ment  | 

1 Answer 1

Reset to default 12

Digging in Google I found:

You can add HTML using

import streamlit as st

st.markdown(html_string, unsafe_allow_html=True)

but this can't add JavaScript.

Forum: How to render already prepared html code with Streamlit?


You can add HTML and JavaScript using

import streamlit.ponents.v1 as ponents

ponents.html(html_string)

but this puts it in <iframe>

Doc: Components API reference


Minimal working example

import streamlit as st
import streamlit.ponents.v1 as ponents

html_string = '''
<h1>HTML string in RED</h1>

<script language="javascript">
  document.querySelector("h1").style.color = "red";
  console.log("Streamlit runs JavaScript");
  alert("Streamlit runs JavaScript");
</script>
'''

ponents.html(html_string)  # JavaScript works

st.markdown(html_string, unsafe_allow_html=True)  # JavaScript doesn't work

To put directly HTML with JavaScript you would need to build Component but it seems more plex method which may need code in TypeScript.

See videos in doc: Create a Streamlit Component

发布评论

评论列表(0)

  1. 暂无评论