Created
February 9, 2022 21:15
-
-
Save aghasemi/d5a6b975b20e17f6f6410d3b47502068 to your computer and use it in GitHub Desktop.
A way to send a Toast message in streamlit and using the Toastify.js library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import streamlit.components.v1 as components | |
_ = components.html( | |
""" | |
<script> | |
function loadScript(url) | |
{ | |
return new Promise(function(resolve, reject) { | |
//Add the script to the main page, not the component iframe | |
const doc = window.parent.document; | |
var head = doc.getElementsByTagName('head')[0]; | |
var script = doc.createElement('script'); | |
script.type = 'text/javascript'; | |
script.src = url; | |
script.async = false | |
script.onreadystatechange = resolve; | |
script.onload = resolve; | |
// Fire the loading | |
head.appendChild(script); | |
}); | |
} | |
function loadCSS(url) | |
{ | |
return new Promise(function(resolve, reject) { | |
// Same thing about the main page | |
const doc = window.parent.document; | |
var head = doc.getElementsByTagName('head')[0]; | |
let cssLink = doc.createElement("link"); | |
cssLink.setAttribute("rel", "stylesheet"); | |
cssLink.setAttribute("type", "text/css"); | |
cssLink.setAttribute("href", url); | |
cssLink.onreadystatechange = resolve; | |
cssLink.onload = resolve; | |
head.appendChild(cssLink); | |
}); | |
} | |
loadScript('https://cdn.jsdelivr.net/npm/toastify-js').then( () => { | |
console.log('Script loaded') | |
loadCSS("https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css").then( () => { | |
console.log('CSS Loaded') | |
// Prefix the Toastify object with `parent.` since we are in the iframe | |
parent.Toastify({ | |
text: "This is a test toast", | |
duration: 3000, | |
destination: "https://github.com/apvarun/toastify-js", | |
newWindow: true, | |
close: true, | |
gravity: "top", // `top` or `bottom` | |
position: "left", // `left`, `center` or `right` | |
stopOnFocus: true, // Prevents dismissing of toast on hover | |
}).showToast(); | |
}) | |
}) | |
</script> | |
""", | |
height=0, | |
width=0, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment