-
Notifications
You must be signed in to change notification settings - Fork 100
python: bypass plotnine auto-closing comms #7657
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,9 +23,10 @@ | |
from ipykernel.zmqshell import ZMQDisplayPublisher, ZMQInteractiveShell | ||
from IPython.core import magic_arguments, oinspect, page | ||
from IPython.core.error import UsageError | ||
from IPython.core.formatters import DisplayFormatter, IPythonDisplayFormatter, catch_format_error | ||
from IPython.core.interactiveshell import ExecutionInfo, ExecutionResult, InteractiveShell | ||
from IPython.core.magic import Magics, MagicsManager, line_magic, magics_class | ||
from IPython.utils import PyColorize | ||
from IPython.utils import PyColorize, dir2 | ||
|
||
from .access_keys import encode_access_key | ||
from .connections import ConnectionsService | ||
|
@@ -220,11 +221,48 @@ def connection_show(self, line: str) -> None: | |
original_showwarning = warnings.showwarning | ||
|
||
|
||
class PositronDisplayFormatter(DisplayFormatter): | ||
@traitlets.default("ipython_display_formatter") | ||
def _default_formatter(self): | ||
return PositronIPythonDisplayFormatter(parent=self) | ||
|
||
|
||
class PositronIPythonDisplayFormatter(IPythonDisplayFormatter): | ||
print_method = traitlets.ObjectName("_ipython_display_") | ||
_return_type = (type(None), bool) | ||
|
||
@catch_format_error | ||
def __call__(self, obj): | ||
"""Compute the format for an object.""" | ||
try: | ||
if obj.__module__ == "plotnine.ggplot": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're special casing plotnine here since there is autoplot opening/closing code that causes Positron's plots comm to be closed and the plot gets sent over a more vanilla We've been able to patch other packages in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a good solution! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems totally reasonable to me given we already are doing non-standard stuff with the comms. |
||
obj.draw(show=True) | ||
return True | ||
except AttributeError: | ||
pass | ||
if self.enabled: | ||
# lookup registered printer | ||
try: | ||
printer = self.lookup(obj) | ||
except KeyError: | ||
pass | ||
else: | ||
printer(obj) | ||
return True | ||
# Finally look for special method names | ||
method = dir2.get_real_method(obj, self.print_method) | ||
if method is not None: | ||
method() | ||
return True | ||
return True | ||
Comment on lines
+243
to
+257
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we call |
||
|
||
|
||
class PositronShell(ZMQInteractiveShell): | ||
kernel: PositronIPyKernel | ||
object_info_string_level: int | ||
magics_manager: MagicsManager | ||
display_pub: ZMQDisplayPublisher | ||
display_formatter: PositronDisplayFormatter = traitlets.Instance(PositronDisplayFormatter) # type: ignore | ||
|
||
inspector_class: type[PositronIPythonInspector] = traitlets.Type( | ||
PositronIPythonInspector, # type: ignore | ||
|
@@ -296,6 +334,10 @@ def init_user_ns(self): | |
} | ||
) | ||
|
||
def init_display_formatter(self): | ||
self.display_formatter = PositronDisplayFormatter(parent=self) | ||
self.configurables.append(self.display_formatter) # type: ignore IPython type annotation is wrong | ||
|
||
def _handle_pre_run_cell(self, info: ExecutionInfo) -> None: | ||
"""Prior to execution, reset the user environment watch state.""" | ||
# If an empty cell is being executed, do nothing. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ matplotlib | |
numpy | ||
pandas | ||
plotly | ||
plotnine | ||
polars | ||
polars[timezone]; sys_platform == 'win32' | ||
pyarrow | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this also be wrapped in
if self.enabled
? I'm not really sure how these work.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking into this! I am also seeing different plots than anticipated for the retina setting in plotnine, which is maybe related 👀