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

IPython display compatibility in pyodide builds #4270

Merged
merged 1 commit into from
Jan 11, 2023
Merged
Changes from all 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
15 changes: 15 additions & 0 deletions panel/io/mime_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ def _convert_expr(expr: ast.Expr) -> ast.Expression:
expr.col_offset = 0
return ast.Expression(expr.value, lineno=0, col_offset = 0)

_OUT_BUFFER = []

def _display(*objs, **kwargs):
"""
IPython.display compatibility wrapper.

Note: This only handles a single display.
"""
_OUT_BUFFER.extend(list(objs))

def exec_with_return(
code: str,
global_context: Dict[str, Any] = None,
Expand Down Expand Up @@ -151,6 +161,7 @@ def exec_with_return(
The return value of the executed code.
"""
global_context = global_context if global_context else globals()
global_context['display'] = _display
code_ast = ast.parse(code)

init_ast = copy.deepcopy(code_ast)
Expand All @@ -173,9 +184,13 @@ def exec_with_return(
out = None
if code.strip().endswith(';'):
out = None
if _OUT_BUFFER and out is None:
out = _OUT_BUFFER[-1]
except Exception:
out = None
traceback.print_exc(file=stderr)
finally:
_OUT_BUFFER.clear()
return out

#---------------------------------------------------------------------
Expand Down