Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[notebook] Make presentation_id consistent and use data-attribute for rrd #1881

Merged
merged 3 commits into from
Apr 17, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions rerun_py/rerun_sdk/rerun/recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,35 @@ def as_html(
if app_location is None:
app_location = bindings.get_app_url()

random_string = "".join(random.choice(string.ascii_letters) for i in range(6))
# Use a random presentation ID to avoid collisions when multiple recordings are shown in the same notebook.
presentation_id = "".join(random.choice(string.ascii_letters) for i in range(6))

base64_data = base64.b64encode(self.storage.get_rrd_as_bytes()).decode("utf-8")

html_template = f"""
<div id="{random_string}_rrd" style="display: none;">{base64_data}</div>
<div id="{random_string}_error" style="display: none;"><p>Timed out waiting for {app_location} to load.</p>
<div id="{presentation_id}_rrd" style="display: none;" data-rrd="{base64_data}"></div>
<div id="{presentation_id}_error" style="display: none;"><p>Timed out waiting for {app_location} to load.</p>
<p>Consider using <code>rr.self_host_assets()</code></p></div>
<script>
timeout_{random_string} = setTimeout(() => {{
document.getElementById("{random_string}_error").style.display = 'block';
{presentation_id}_timeout = setTimeout(() => {{
document.getElementById("{presentation_id}_error").style.display = 'block';
}}, {timeout_ms});

window.addEventListener("message", function(rrd) {{
return async function onIframeReady_{random_string}(event) {{
var iframe = document.getElementById("{random_string}");
return async function {presentation_id}_onIframeReady(event) {{
var iframe = document.getElementById("{presentation_id}_iframe");
if (event.source === iframe.contentWindow) {{
clearTimeout(timeout_{random_string});
document.getElementById("{random_string}_error").style.display = 'none';
clearTimeout({presentation_id}_timeout);
document.getElementById("{presentation_id}_error").style.display = 'none';
iframe.style.display = 'inline';
window.removeEventListener("message", onIframeReady_{random_string});
window.removeEventListener("message", {presentation_id}_onIframeReady);
iframe.contentWindow.postMessage((await rrd), "*");
}}
}}
}}(async function() {{
await new Promise(r => setTimeout(r, 0));
var div = document.getElementById("{random_string}_rrd");
var base64Data = div.textContent;
var div = document.getElementById("{presentation_id}_rrd");
var base64Data = div.dataset.rrd;
var intermediate = atob(base64Data);
var buff = new Uint8Array(intermediate.length);
for (var i = 0; i < intermediate.length; i++) {{
Expand All @@ -70,7 +71,8 @@ def as_html(
return buff;
}}()));
</script>
<iframe id="{random_string}" width="{width}" height="{height}" src="{app_location}?url=web_event://&persist=0"
<iframe id="{presentation_id}_iframe" width="{width}" height="{height}"
src="{app_location}?url=web_event://&persist=0"
frameborder="0" style="display: none;" allowfullscreen=""></iframe>
"""

Expand Down