Skip to content

Commit

Permalink
fix: address dynamic resizing of iframes on ipython (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
krishnanand5 authored Oct 17, 2024
1 parent 3d7b1ea commit 3159fc1
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions maidr/core/maidr.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import annotations
varfrom __future__ import annotations

import io
import json
Expand Down Expand Up @@ -189,22 +189,45 @@ def _inject_plot(plot: HTML, maidr: str) -> Tag:
tags.script(maidr),
)

def generate_iframe_script(unique_id: str) -> str:
resizing_script = f"""
function resizeIframe() {{
let iframe = document.getElementById('{unique_id}');
if (iframe && iframe.contentWindow && iframe.contentWindow.document) {{
let iframeDocument = iframe.contentWindow.document;
let brailleContainer = iframeDocument.getElementById('braille-div');
let height = iframeDocument.body.scrollHeight;
if (brailleContainer && brailleContainer === iframeDocument.activeElement) {{
height *= 1.35; # Increase height by 35% if braille-container is in focus
}}
iframe.style.height = (height + 150) + 'px';
iframe.style.width = iframeDocument.body.scrollWidth + 'px';
}}
}}
let iframe = document.getElementById('{unique_id}');
iframe.onload = function() {{
resizeIframe();
iframe.contentWindow.addEventListener('resize', resizeIframe);
iframe.contentWindow.document.addEventListener('focusin', resizeIframe);
iframe.contentWindow.document.addEventListener('focusout', resizeIframe);
}};
"""
return resizing_script

unique_id = "iframe_" + Maidr._unique_id()

resizing_script = generate_iframe_script(unique_id)

# Embed the rendering into an iFrame for proper working of JS library.
base_html = tags.iframe(
id=unique_id,
srcdoc=str(base_html.get_html_string()),
width="100%",
height="100%",
scrolling="auto",
style="background-color: #fff",
scrolling="no",
style="background-color: #fff; position: relative; border: none",
frameBorder=0,
onload="""
this.style.height = this.contentWindow.document.body.scrollHeight +
100 + 'px';
"""
+ Environment.initialize_llm_secrets(unique_id),
onload=resizing_script,
)

return base_html

0 comments on commit 3159fc1

Please sign in to comment.