Guest User

Untitled

a guest
May 5th, 2024
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. import base64
  2.  
  3. import requests
  4. import streamlit as st
  5. import streamlit.components.v1 as components
  6.  
  7.  
  8. st.set_page_config(layout="wide")
  9.  
  10.  
  11. def get_data_img(img_fp: str):
  12. if img_fp.startswith("http"):
  13. content = requests.get(img_fp).content
  14. else:
  15. with open(img_fp, "rb") as f:
  16. content = f.read()
  17.  
  18. return base64.b64encode(content).decode("utf-8")
  19.  
  20.  
  21. # data_url = get_data_img("mathisfun.png")
  22. # type_img = "png"
  23. data_url = get_data_img("https://www.mathsisfun.com/images/style/logo.svg")
  24. type_img = "svg+xml"
  25.  
  26. # with JS
  27. components.html(
  28. f"""
  29. <!DOCTYPE html>
  30. <html lang="en">
  31. <head>
  32. <script>
  33. function loadImg(data_url)
  34. {{
  35. var itemPrev = document.getElementById("ItemPreview");
  36. itemPrev.src = "data:image/{type_img};base64," + data_url;
  37. }}
  38. </script>
  39. <meta charset="UTF-8">
  40. <title>Test Image</title>
  41. </head>
  42. <body onload="loadImg('{data_url}')">
  43. <img id="ItemPreview" src="">
  44. </body>
  45. </html>
  46. """,
  47. height=200,
  48. )
  49.  
  50. # without JS
  51. # components.html(
  52. # f"""
  53. # <!DOCTYPE html>
  54. # <html lang="en">
  55. # <head>
  56. # <meta charset="UTF-8">
  57. # <title>Test Image</title>
  58. # </head>
  59. # <body>
  60. # <img src="data:image/{type_img};base64,{data_url}">
  61. # </body>
  62. # </html>
  63. # """,
  64. # height=200,
  65. # )
  66.  
Add Comment
Please, Sign In to add comment