-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement: patch
IPython.display.display
to call `mo.output.appen…
…d` (#919) * initial impl, test failing * fix tests * lint * add ipython as optional dep * ipython version
- Loading branch information
Showing
10 changed files
with
139 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2024 Marimo. All rights reserved. | ||
from __future__ import annotations | ||
|
||
import functools | ||
from typing import Any, Callable | ||
|
||
from marimo._messaging.mimetypes import KnownMimeType | ||
from marimo._output import builder | ||
from marimo._output.formatters.formatter_factory import FormatterFactory | ||
|
||
|
||
class IPythonFormatter(FormatterFactory): | ||
@staticmethod | ||
def package_name() -> str: | ||
return "IPython" | ||
|
||
def register(self) -> Callable[[], None]: | ||
import IPython.display # type:ignore | ||
|
||
from marimo._output import formatting | ||
from marimo._runtime.output import _output | ||
|
||
old_display = IPython.display.display | ||
# monkey patch IPython.display.display, which imperatively writes | ||
# outputs to the frontend | ||
|
||
@functools.wraps(old_display) | ||
def display(*objs: Any, **kwargs: Any) -> None: | ||
# IPython.display.display returns a DisplayHandle, which | ||
# can be used to update the displayed object. We don't support | ||
# that yet ... | ||
if kwargs.pop("clear", False): | ||
_output.clear() | ||
for value in objs: | ||
_output.append(value) | ||
|
||
IPython.display.display = display | ||
|
||
def unpatch() -> None: | ||
IPython.display.display = old_display | ||
|
||
@formatting.formatter(IPython.display.HTML) | ||
def _format_html( | ||
html: IPython.display.HTML, | ||
) -> tuple[KnownMimeType, str]: | ||
if html.url is not None: | ||
# TODO(akshayka): resize iframe not working | ||
data = builder.h.iframe( | ||
src=html.url, | ||
width="100%", | ||
onload="__resizeIframe(this)", | ||
scrolling="auto", | ||
frameborder="0", | ||
) | ||
else: | ||
data = str(html._repr_html_()) # type: ignore | ||
|
||
return ("text/html", data) | ||
|
||
return unpatch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Copyright 2024 Marimo. All rights reserved. | ||
import marimo | ||
|
||
__generated_with = "0.3.1" | ||
app = marimo.App() | ||
|
||
|
||
@app.cell | ||
def __(): | ||
import IPython | ||
import marimo as mo | ||
|
||
url = IPython.display.HTML("https://marimo.io") | ||
url | ||
return IPython, mo, url | ||
|
||
|
||
@app.cell | ||
def __(IPython): | ||
html = IPython.display.HTML("<em>hello world</em>") | ||
html | ||
return html, | ||
|
||
|
||
@app.cell | ||
def __(IPython, html, url): | ||
IPython.display.display(html, url) | ||
return | ||
|
||
|
||
@app.cell | ||
def __(): | ||
# not on PyPI | ||
# installation instructions here https://github.com/allefeld/pytikz | ||
import tikz | ||
return tikz, | ||
|
||
|
||
@app.cell | ||
def __(tikz): | ||
# define coordinates as a list of tuples | ||
coords = [(0, 0), (0, 2), (1, 3.25), (2, 2), (2, 0), (0, 2), (2, 2), (0, 0), (2, 0)] | ||
|
||
# create `Picture` object | ||
pic = tikz.Picture() | ||
# draw a line following the coordinates | ||
pic.draw(tikz.line(coords), thick=True, rounded_corners='4pt') | ||
return coords, pic | ||
|
||
|
||
@app.cell | ||
def __(pic): | ||
pic.demo(dpi=300) | ||
return | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from __future__ import annotations | ||
|
||
import importlib | ||
import os.path | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters