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

Set JPY_SESSION_NAME to full notebook path. #1100

Merged
merged 4 commits into from Jan 12, 2023

Conversation

Carreau
Copy link
Contributor

@Carreau Carreau commented Nov 28, 2022

This also add some typing here and there, and extend one of the console warning to log an exception when there is an error.

My main concern is that get_kernel_env need to become async.

@codecov
Copy link

codecov bot commented Nov 28, 2022

Codecov Report

Base: 79.93% // Head: 79.99% // Increases project coverage by +0.05% 🎉

Coverage data is based on head (6c7a897) compared to base (a52709c).
Patch coverage: 91.30% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1100      +/-   ##
==========================================
+ Coverage   79.93%   79.99%   +0.05%     
==========================================
  Files          68       68              
  Lines        8124     8143      +19     
  Branches     1601     1604       +3     
==========================================
+ Hits         6494     6514      +20     
+ Misses       1205     1202       -3     
- Partials      425      427       +2     
Impacted Files Coverage Δ
jupyter_server/services/sessions/handlers.py 79.20% <40.00%> (-1.95%) ⬇️
jupyter_server/services/kernels/kernelmanager.py 81.87% <90.00%> (+0.70%) ⬆️
jupyter_server/base/handlers.py 78.82% <100.00%> (ø)
jupyter_server/gateway/managers.py 83.37% <100.00%> (ø)
jupyter_server/services/contents/fileio.py 90.10% <100.00%> (ø)
jupyter_server/services/sessions/sessionmanager.py 88.36% <100.00%> (+0.36%) ⬆️
jupyter_server/utils.py 85.46% <100.00%> (+0.17%) ⬆️
jupyter_server/serverapp.py 80.19% <0.00%> (+0.26%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Member

@kevin-bates kevin-bates left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Carreau. I don't see an issue with making get_kernel_env() async since its essentially an internal method (i.e., so custom (and synchronous) SessionManagers should still work calling their "internal" implementation). I suppose things could be dicey if there is a server extension that is calling that method on the active SessionManager, but, hey, "major release boundaries" have their benefits. 😉

jupyter_server/services/sessions/handlers.py Outdated Show resolved Hide resolved
jupyter_server/services/sessions/handlers.py Outdated Show resolved Hide resolved
jupyter_server/services/kernels/kernelmanager.py Outdated Show resolved Hide resolved
"Create a uuid for a new session"
return str(uuid.uuid4())

async def create_session(
self, path=None, name=None, type=None, kernel_name=None, kernel_id=None
):
"""Creates a session and returns its model"""
"""Creates a session and returns its model

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add parameter descriptions for the others, or remove the description for name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the description of what I was sure of, but yes in general many of those function should get actual documentation.

jupyter_server/services/sessions/sessionmanager.py Outdated Show resolved Hide resolved
Comment on lines 288 to 314
if name is not None:
session_dir = await self.contents_manager.get_kernel_path(path=path)
cwd = self.kernel_manager.cwd_for_path(path)
path = os.path.join(cwd, session_dir, name)
assert isinstance(path, str)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't path be the same value as name when the older form of the body is used (i.e., the one that includes a 'notebook' stanza)? And, if so, will that produce the expected results?

Any idea how "old" the old API is? I.e., does lab or nbclassic use that form of payload?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly don't know. It's honestly really unclear for me why this is still called path if it's often (always?) not a path.

I'm also quite confused about the on-disk Path and Api Path which is why I started to add type annotations.

@Carreau Carreau force-pushed the notebook-name branch 3 times, most recently from 0b3e4d5 to 2c0ddb0 Compare November 30, 2022 13:20
@blink1073
Copy link
Collaborator

Ah, I have to fix CI separately for changes in pytest-jupyter

@blink1073
Copy link
Collaborator

@blink1073
Copy link
Collaborator

@Carreau
Copy link
Contributor Author

Carreau commented Nov 30, 2022

Ah, I have to fix CI separately for changes in pytest-jupyter

Thanks, some of the original failures were due to changes I've made, so no problem.

Comment on lines 302 to 315
if name is not None:
session_dir = await ensure_async(self.contents_manager.get_kernel_path(path=path))
cwd = self.kernel_manager.cwd_for_path(path)
path = os.path.join(cwd, session_dir, name)
assert isinstance(path, str)
return {**os.environ, "JPY_SESSION_NAME": path}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think session_dir is necessary here. If the notebook file is in a subdirectory of root_dir, JPY_SESSION_NAME will contain a redundant directory name...
Screen Shot 2022-11-30 at 12 34 26 PM

Removing session_dir from the equation would allow the method to be synchronous again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is true, thanks.

@Carreau
Copy link
Contributor Author

Carreau commented Dec 1, 2022

pre-commit (pyupgrade) seem to insist on having optional types be X|Y which is only python 3.10 plus AFAICT.

@blink1073
Copy link
Collaborator

That sounds like a bug, we have it configured to --py38-plus.

@blink1073
Copy link
Collaborator

Okay, build failures seem fixed.

@Carreau
Copy link
Contributor Author

Carreau commented Dec 1, 2022

Okay, build failures seem fixed.

Do you want me to rebase / squash ?

@blink1073
Copy link
Collaborator

No thanks, squash is the default merge type

@blink1073
Copy link
Collaborator

Note: we're not merging any API-changing PRs until after the 2.0 final release on Monday.

@blink1073
Copy link
Collaborator

Heads up, this PR picked up a few conflicts from the linting refactor in #1114

@Carreau
Copy link
Contributor Author

Carreau commented Dec 22, 2022

The linting fails but this is unrelated to this PR, some class have signature that change between the sub and superclass, in particular

async def _async_start_kernel(self, kernel_id=None,... with kernel_name in **kwargs, while in superclass (in jupyter_client) it is the opposite with kernel_name as first parameter and kernel_id in kwargs...

@Carreau
Copy link
Contributor Author

Carreau commented Dec 22, 2022

There is also am async method in a subclass that is not async in superclass, and comparison between list and strings that is always false.

Carreau and others added 4 commits January 11, 2023 10:28
This also add some typing here and there, and extend one of the console
warning to log an exception when there is an error.

My main concern is that get_kernel_env need to become async.

Co-authored-by: Kevin Bates <kbates4@gmail.com>
@Carreau
Copy link
Contributor Author

Carreau commented Jan 11, 2023

Rebased on master, all the typing error seem unrelated. Is there anything else I can do to help getting that in ?

@@ -196,12 +199,16 @@ async def _remove_kernel_when_ready(self, kernel_id, kernel_awaitable):
self._kernel_connections.pop(kernel_id, None)
self._kernel_ports.pop(kernel_id, None)

async def _async_start_kernel(self, kernel_id=None, path=None, **kwargs):
# TODO DEC 2022: Revise the type-ignore once the signatures have been changed upstream
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still todo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR have been merged, but we likely need it to be released, and we may want o wait a bit to keep compat with older versions, and once we pin to a newer jupyter_client, we can force this one to be positional only as well.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, let's move ahead, thanks!

@blink1073 blink1073 merged commit 3d516b3 into jupyter-server:main Jan 12, 2023
@Carreau
Copy link
Contributor Author

Carreau commented Jan 12, 2023 via email

Carreau added a commit to Carreau/ipykernel that referenced this pull request Jan 24, 2023
Following jupyter-server/jupyter_server#1100, this expose the value of
JUPYTER_SESSION_NAME environment variable as `__file__`. In _most_
cases, this should reflect the notebook file that correspond to current
kernel.

It is not set when the env variable is not set. And it represent the
name of file at the time the session was created, it will not reflect
renaming of the file.

We could support renaming, but that would require the frontend to send
the name of the current file; this would be helpful when we have
multiple frontends/documents connected to the same kernel, but this
would be out of scope for this PR and a longer discussion to have.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants