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

feat(maidr.show): support interactive maidr plots within Jupyter Notebooks, VS Code, Google Colab, and Quarto #64

Merged
merged 13 commits into from
Aug 21, 2024
2 changes: 1 addition & 1 deletion maidr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
lineplot,
scatterplot,
)
from .maidr import close, save_html, show, stacked
from .api import close, save_html, show, stacked

__all__ = [
"close",
Expand Down
File renamed without changes.
23 changes: 22 additions & 1 deletion maidr/core/maidr.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import annotations

from typing import Literal

import io
Expand All @@ -12,6 +13,7 @@

from maidr.core.context_manager import HighlightContextManager
from maidr.core.plot import MaidrPlot
from maidr.utils.environment import Environment


class Maidr:
Expand Down Expand Up @@ -161,7 +163,8 @@ def _unique_id() -> str:
@staticmethod
def _inject_plot(plot: HTML, maidr: str) -> Tag:
"""Embed the plot and associated MAIDR scripts into the HTML structure."""
return tags.html(

base_html = tags.html(
tags.head(
tags.meta(charset="UTF-8"),
tags.title("MAIDR"),
Expand All @@ -179,3 +182,21 @@ def _inject_plot(plot: HTML, maidr: str) -> Tag:
),
tags.script(maidr),
)

if Environment.is_interactive_shell():
# If running in an interactive environment (e.g., Jupyter Notebook),
# display the HTML content using an iframe to ensure proper rendering
# and interactivity. The iframe's height is dynamically adjusted
base_html = tags.iframe(
srcdoc=str(base_html.get_html_string()),
width="100%",
height="100%",
scrolling="auto",
style="background-color: #fff",
frameBorder=0,
onload="""
this.style.height = this.contentWindow.document.body.scrollHeight + 100 + 'px';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Daksh,

When I worked on the py-shiny renderer, I faced a UI/UX issue when displaying the generated MAIDR plot on the shiny component. The major problem with the behaviour I observed on Shiny was that the overflow properties were triggered if pre-defined styles were used.

Can you share your findings on how Python notebooks renders the cells? After looking at the documentation, I can see that style customizations are allowed for Python notebooks. Does overflow get applied to the plot if pre-defined styles are employed?

Regards,
Krishna Anandan Ganesan.

""",
)

return base_html
13 changes: 13 additions & 0 deletions maidr/utils/environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Environment:
@staticmethod
def is_interactive_shell() -> bool:
"""Return True if the environment is an interactive shell."""
try:
from IPython.core.interactiveshell import InteractiveShell

return (
InteractiveShell.initialized()
and InteractiveShell.instance() is not None
)
except ImportError:
return False
Loading