-
Notifications
You must be signed in to change notification settings - Fork 11
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
Capture caller frame of napari.run() when starting the console. #18
Conversation
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.
napari/napari#4212 has merged now, should we merge this too @Carreau? Changes look good to me, although there are a few strange extra blank lines :)
This comment was marked as outdated.
This comment was marked as outdated.
665816b
to
845f4fd
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #18 +/- ##
==========================================
+ Coverage 87.60% 90.38% +2.78%
==========================================
Files 4 4
Lines 121 156 +35
==========================================
+ Hits 106 141 +35
Misses 15 15 ☔ View full report in Codecov by Sentry. |
I installed this PR branch and napari
I was expecting |
as we inspect frames my guess is we see the frame at the point the viewer
was created so before C ?
…On Wed, Dec 21, 2022 at 21:48 Peter Sobolewski ***@***.***> wrote:
I installed this PR branch and napari main. No more errors, but I must be
missing something, because, I made a script:
import napari
a = 3
b = 4
viewer = napari.Viewer()
c = 5
napari.run()
I was expecting c to be available in the napari console, but it's not.
🤔
—
Reply to this email directly, view it on GitHub
<#18 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACR5TZJI3BOYHNNT6OGJLLWONULLANCNFSM5QDP6SXA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Ah, I assumed it would be stuff after the viewer. |
Should fix napari/napari#4098 Replaces napari/napari#4140, and see discussion there as well Needs napari/napari#4212
Yeah, I forgot to push... |
Or technically I pushed, but did not commit, so it pushed nothing. |
that would be great too, but probably more complicated cause it requires back and forth communication? |
Ah! I'm so pleased it wasn't me screwing something up again! 😊
|
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.
I have mixed feelings. It is really nice in the context of scripts or notebooks.
But for example, PartSeg allow opening napari viewer from the view menu. And with this PR the context of this method is pushed:
I do not feel that it is proper behavior.
napari_console/qt_console.py
Outdated
@@ -55,6 +58,14 @@ def str_to_rgb(arg): | |||
asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy()) | |||
|
|||
|
|||
def _not_napari(n: int, frame: FrameType): | |||
# in-n-out is used in napari for dependency injection. | |||
for pref in ["napari", "in_n_out"]: |
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.
What with modules named napari-something
?
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 we change these to
for pref in ["napari", "in_n_out"]: | |
for pref in ["napari.", "in_n_out."]: |
then?
@Czaki can you elaborate what is the issue with your PartSeg use case? |
exporting internal application variables to the console. (It could be easily workaround - for a person like me who understands napari internals)
I use it regularly in the context of debugging and development. And some person uses it to perform actions not possible by GUI (ex layer scale). |
Gotcha. Makes sense. Maybe we need an argument to not do this when launching napari? |
Can we try to push this over the line? Todos:
For point 2, I wouldn't mind leaving it to a later PR, as this is already extremely useful. |
I like option 1. I'm not the favorite of option 2. It should be rather information in the documentation that will inform about this and suggest ways to workaround it. (maybe such variable |
# Description Currently, if [`update_console`](https://github.com/napari/napari/blob/7117d22e4baa740864762454911dc5ed09764ccc/napari/viewer.py#L88) is called on viewer object before the console is instantiated the function does nothing. With this PR instead references to the requested variables are added to a `console_backlog` list attribute on qt_viewer. If and when console is instantiated these variables are pushed. Unsure if [this related PR](napari/napari-console#18) is moving forward and whether it would solve this case. ## Type of change - [x] Enhancment # References Closes #4098 # How has this been tested? - [x] Ran unit tests for `napari/_qt`. There were some errors but looked unrelated to my changes. - [x] Added unit test based on existing `test_update_console` ## Final checklist: - [x] My PR is the minimum possible work for the desired functionality - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] I have added tests that prove my fix is effective or that my feature works --------- Co-authored-by: Peter Sobolewski <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Grzegorz Bokota <[email protected]> Co-authored-by: Juan Nunez-Iglesias <[email protected]>
Currently, if [`update_console`](https://github.com/napari/napari/blob/7117d22e4baa740864762454911dc5ed09764ccc/napari/viewer.py#L88) is called on viewer object before the console is instantiated the function does nothing. With this PR instead references to the requested variables are added to a `console_backlog` list attribute on qt_viewer. If and when console is instantiated these variables are pushed. Unsure if [this related PR](napari/napari-console#18) is moving forward and whether it would solve this case. - [x] Enhancment Closes #4098 - [x] Ran unit tests for `napari/_qt`. There were some errors but looked unrelated to my changes. - [x] Added unit test based on existing `test_update_console` - [x] My PR is the minimum possible work for the desired functionality - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] I have added tests that prove my fix is effective or that my feature works --------- Co-authored-by: Peter Sobolewski <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Grzegorz Bokota <[email protected]> Co-authored-by: Juan Nunez-Iglesias <[email protected]>
Currently, if [`update_console`](https://github.com/napari/napari/blob/7117d22e4baa740864762454911dc5ed09764ccc/napari/viewer.py#L88) is called on viewer object before the console is instantiated the function does nothing. With this PR instead references to the requested variables are added to a `console_backlog` list attribute on qt_viewer. If and when console is instantiated these variables are pushed. Unsure if [this related PR](napari/napari-console#18) is moving forward and whether it would solve this case. - [x] Enhancment Closes #4098 - [x] Ran unit tests for `napari/_qt`. There were some errors but looked unrelated to my changes. - [x] Added unit test based on existing `test_update_console` - [x] My PR is the minimum possible work for the desired functionality - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] I have added tests that prove my fix is effective or that my feature works --------- Co-authored-by: Peter Sobolewski <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Grzegorz Bokota <[email protected]> Co-authored-by: Juan Nunez-Iglesias <[email protected]>
Currently, if [`update_console`](https://github.com/napari/napari/blob/7117d22e4baa740864762454911dc5ed09764ccc/napari/viewer.py#L88) is called on viewer object before the console is instantiated the function does nothing. With this PR instead references to the requested variables are added to a `console_backlog` list attribute on qt_viewer. If and when console is instantiated these variables are pushed. Unsure if [this related PR](napari/napari-console#18) is moving forward and whether it would solve this case. - [x] Enhancment Closes #4098 - [x] Ran unit tests for `napari/_qt`. There were some errors but looked unrelated to my changes. - [x] Added unit test based on existing `test_update_console` - [x] My PR is the minimum possible work for the desired functionality - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] I have added tests that prove my fix is effective or that my feature works --------- Co-authored-by: Peter Sobolewski <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Grzegorz Bokota <[email protected]> Co-authored-by: Juan Nunez-Iglesias <[email protected]>
Currently, if [`update_console`](https://github.com/napari/napari/blob/7117d22e4baa740864762454911dc5ed09764ccc/napari/viewer.py#L88) is called on viewer object before the console is instantiated the function does nothing. With this PR instead references to the requested variables are added to a `console_backlog` list attribute on qt_viewer. If and when console is instantiated these variables are pushed. Unsure if [this related PR](napari/napari-console#18) is moving forward and whether it would solve this case. - [x] Enhancment Closes #4098 - [x] Ran unit tests for `napari/_qt`. There were some errors but looked unrelated to my changes. - [x] Added unit test based on existing `test_update_console` - [x] My PR is the minimum possible work for the desired functionality - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] I have added tests that prove my fix is effective or that my feature works --------- Co-authored-by: Peter Sobolewski <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Grzegorz Bokota <[email protected]> Co-authored-by: Juan Nunez-Iglesias <[email protected]>
If you yourself are happy with your proposal can you dismiss your review and we can merge? 😃 |
@jni I have added tests and updated the readme. Could you check it? |
Well the tests seem unhappy... 😅 |
I forget that there is a global state in the console, so names in tests cannot collide. |
I updated the README message to include a usage example. |
If you want to disable this behavior (for example, because you are embedding | ||
napari and the console within some larger application), you can add | ||
`NAPARI_EMBED=1` to your environment variables before instantiating the | ||
console. |
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.
This is not true. I implement it as global module-level variable, not an environment variable. Should I change it?
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.
Oh! Sorry. I did read that and thought you misspoke. 😅 I think I prefer this way because it's easier to describe and quite a common way to change functionality. Otherwise you have to make sure that the global is accessible in the correct frame, which is not something people are used to thinking about, I think? 🤷
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.
yes, but environment variable is global per process, and module level variable is in module.
If someone would like first spawn PartSeg and then napari, If I use an environment variable, then it will prevent variable propagation in follow-up napari start.
If using module-level variable it impacts only possible console spawn when open console from napari embedded here. But will not impact napari spawned from the top-level script.
Predicates that return Wether we are in napari by looking | ||
at: | ||
1) the frames modules names: | ||
2) the min_depth |
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.
Predicates that return Wether we are in napari by looking | |
at: | |
1) the frames modules names: | |
2) the min_depth | |
Determines whether we are in napari by looking at: | |
1) the frames modules names: | |
2) the min_depth |
(from me at #18 (comment)) @Carreau it looks like that was added by you in napari/napari#2677 without any discussion. I see now in the PR description your comment, "I also inject action_manager in the qtconsole context, so one does not have to import it everytime", but in general I think the action manager is not really a user-facing object, even though it is convenient for people developing specific things. Was that for debugging and can it be removed? (This would be for 0.5.0 or later in any case...) |
I belive this was done before we had the shortcut editor so we needed a way to edit shortcuts is a relatively friendly way. |
Well, I suggest that we merge this and fix the action_manager injection in another PR. |
(It would be cool to simul-release this improvement with 0.4.19!) |
Shit, I should have tested this again before merging 😅 Right now when I run
I wonder if this commit broke things? |
Indeed changing this:
to
fixes it again. I'll make a PR, since I don't think the first part is necessary (?) @Czaki |
Should fix napari/napari#4098
Replaces napari/napari#4140, and see discussion there as well
Needs napari/napari#4212