Skip to content

Commit

Permalink
IPython display compatibility in pyodide builds (#4270)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Jan 11, 2023
1 parent 5a39b76 commit 5a69995
Showing 1 changed file with 15 additions and 0 deletions.
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

0 comments on commit 5a69995

Please sign in to comment.