Skip to content

Commit

Permalink
fixed using pylustrator with current jupyter notebook versions (fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerum committed Jun 6, 2022
1 parent c52aaf4 commit 225e208
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pylustrator/jupyter_cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ def getIpythonCurrentCell() -> str:
import inspect
# get the first stack which has a filename starting with "<ipython-input" (e.g. an ipython cell) and from
# this stack get the globals, there get the executed cells history and the last element from it
return [stack for stack in inspect.stack() if stack.filename.startswith("<ipython-input")][0][0].f_globals["_ih"][-1]
return [stack for stack in inspect.stack() if stack.filename.startswith("<ipython-input") or "tmp/ipykernel" in stack.filename][0][0].f_globals["_ih"][-1]


global_files = {}
build_in_open = open
def open(filename: str, *args, **kwargs):
""" open a file and if its a jupyter cell then mock a filepointer to that cell """
if filename.startswith("<ipython"):
if filename.startswith("<ipython") or "tmp/ipykernel" in filename:
class IPythonCell:
text = None
write_text = None
Expand All @@ -65,7 +65,7 @@ def __init__(self, filename, mode, **kwargs):
self.filename = filename.strip()

if mode == "r":
if self.filename[0] == "<" and self.filename[-1] == ">":
if (self.filename[0] == "<" and self.filename[-1] == ">") or ("tmp/ipykernel" in filename and not filename.endswith(".tmp")):
self.is_cell = True
self.text = getIpythonCurrentCell()
else:
Expand Down Expand Up @@ -93,7 +93,7 @@ def __enter__(self):

def __exit__(self, exc_type, exc_val, exc_tb):
if self.write_text is not None:
if self.filename[0] == "<" and self.filename[-1] == ">":
if self.filename[0] == "<" and self.filename[-1] == ">" or ("tmp/ipykernel" in filename and not filename.endswith(".tmp")):
setJupyterCellText(self.write_text)
else:
global_files[self.filename] = self.write_text
Expand Down

0 comments on commit 225e208

Please sign in to comment.