# https://github.com/snehankekre/streamlit-tensorboard/blob/master/streamlit_tensorboard/__init__.py
import streamlit as st
import streamlit.components.v1 as components
from tensorboard import manager
import shlex
import random
import html
import json
def st_tensorboard(logdir="/logs/", port=6006, width=None, height=800, scrolling=True):
"""Embed Tensorboard within a Streamlit app
Parameters
----------
logdir: str
Directory where TensorBoard will look to find TensorFlow event files that it can display.
TensorBoard will recursively walk the directory structure rooted at logdir, looking for .*tfevents.* files.
Defaults to /logs/
port: int
Port to serve TensorBoard on. Defaults to 6006
width: int
The width of the frame in CSS pixels. Defaults to the report’s default element width.
height: int
The height of the frame in CSS pixels. Defaults to 800.
scrolling: bool
If True, show a scrollbar when the content is larger than the iframe.
Otherwise, do not show a scrollbar. Defaults to True.
Example
-------
>>> st_tensorboard(logdir="/logs/", port=6006, width=1080)
"""
logdir = logdir
port = port
width = width
height = height
frame_id = "tensorboard-frame-{:08x}".format(random.getrandbits(64))
shell = """
"""
#args_string = f"--logdir {logdir} --port {port}"
args_string = f"--logdir {logdir}"
parsed_args = shlex.split(args_string, comments=True, posix=True)
start_result = manager.start(parsed_args)
if isinstance(start_result, manager.StartReused):
port = start_result.info.port
print(f"Reusing TensorBoard on port {port}")
proxy_url = "http://localhost:%PORT%"
proxy_url = proxy_url.replace("%PORT%", "%d" % port)
replacements = [
("%HTML_ID%", html.escape(frame_id, quote=True)),
("%JSON_ID%", json.dumps(frame_id)),
("%HEIGHT%", "%d" % height),
("%PORT%", "0"),
("%URL%", json.dumps(proxy_url)),
]
for (k, v) in replacements:
shell = shell.replace(k, v)
return components.html(shell, width=width, height=height, scrolling=scrolling)