Skip to content

Commit

Permalink
Capture caller frame of napari.run() when starting the console.
Browse files Browse the repository at this point in the history
Should fix napari/napari#4098
Replaces napari/napari#4140, and see discussion there as well
Needs napari/napari#4212
  • Loading branch information
Carreau committed Dec 21, 2022
1 parent ef4e1c2 commit 665816b
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions napari_console/qt_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from qtpy.QtGui import QColor


from napari.utils.naming import CallerFrame

def str_to_rgb(arg):
"""Convert an rgb string 'rgb(x,y,z)' to a list of ints [x,y,z]."""
Expand Down Expand Up @@ -54,6 +55,8 @@ def str_to_rgb(arg):
# fallback to the pre-3.8 default of Selector
asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())

def _not_napari(frame):
return frame.f_globals.get("__name__", '').startswith('napari')

class QtConsole(RichJupyterWidget):
"""Qt view for the console, an integrated iPython terminal in napari.
Expand Down Expand Up @@ -128,6 +131,7 @@ def __init__(self, viewer: 'napari.viewer.Viewer'):
raise ValueError(
'ipython shell not recognized; ' f'got {type(shell)}'
)
self._capture()
# Add any user variables
user_variables = user_variables or {}
self.push(user_variables)
Expand All @@ -140,6 +144,13 @@ def __init__(self, viewer: 'napari.viewer.Viewer'):
# TODO: Try to get console from jupyter to run without a shift click
# self.execute_on_complete_input = True

def _capture(self):
"""
Capture variable from first enclosing scope that is not napari
"""
with CallerFrame(_not_napari) as c:
self.push(dict(c.namespace))

def _update_theme(self, event=None):
"""Update the napari GUI theme."""
from napari.utils.theme import get_theme, template
Expand Down

0 comments on commit 665816b

Please sign in to comment.