Question about porting a v1 jupyter plugin: Our v1...
# plugins
j
Question about porting a v1 jupyter plugin: Our v1 plugin had to set
os.environ[PYTHONPATH] = ":".join(sys.path)
before running the notebook via
jupyter_core.command.main()
. Is this still appropriate to do in v2 or is there a better way. This was needed so the spawned kernel could inherit the path of the targets.
Also, to pass the "command line arguments" I need to set
sys.argv = ['', 'notebook', '--notebook-dir=', '/path/to/repo']
. Is there a way to get the repo root in my script or do I need to figure that out myself?
h
Very similar for setting pythonpath. You’ll set
PEX_EXTRA_SYS_PATH
to the source roots being used, like
src/python
. Pex uses this to extend PYTHONPATH See https://github.com/pantsbuild/pants/blob/ff02ef0ddd284bc5f4d9d4a724c3d8ecbe066854/src/python/pants/backend/python/goals/repl.py#L44-L48 for how that’s done with the normal Python repl
Is there a way to get the repo root in my script
If you’re using the
repl
goal hook, you use
<http://repl.in|repl.in>_chroot()
for this. It’ll ensure that the correct paths are being used. You can mostly copy and paste that file I linked to
j
Hmm... If I use the chroot, then the Notebook file will get saved in a hidden directory. I want the Jupyter Notebook app to have the top of the repo as its root. Or maybe the
dist/
directory. That way it is easier for the eng to work with the file after they are done with the notebook.
re: PEX_EXTRA_SYS_PATH. If I am using the "Pex: Install binaries through pip" way of getting my jupyter binary, I assume I need to modify the env variable in the method that has the
Copy code
async def jupyter_binary(...):
    pex = await Get(...)
code.
h
If I am using the “Pex: Install binaries through pip” way of getting my jupyter binary
The
PEX_EXTRA_SYS_PATH
is only needed when you run the process, not when building the Pex. It gets used at runtime, and isn’t necessary when building the Pex
j
Got it. Thank you for the orientation. 🧭
❤️ 1
h
then the Notebook file will get saved in a hidden directory
Oh, hm, interesting. How does saving the notebook file work? Jupyter will write the file for you? Can you specify where that file gets written?
j
Jupyter Notebook opens by default to the directory that you run the command
jupyter notebook
from. You can add the
--notebook-dir=/path/somewhere
option to set the root to where ever you wish. Once started, the Jupyter Notebook app can't read or write above that directory. The kernel has access to all the paths that were set when it was spawned (I believe).
👍 1
So the kernel can import things on the
PYTHONPATH
but the browser code can't save or open anywhere but
notebook-dir
.
h
@hundreds-breakfast-49010 what’s the story around an
InteractiveProcess
running in the build root, with it writing to arbitrary file paths in the build root? Pants has no restrictions on that, right? This is why we solely allow a
@goal_rule
to run an
InteractiveProcess
, which acts ~like a lock
j
layers upon layers upon layers
🙃 1
h
yeah, an interactive process is run using the rust std library API for spawning a subprocess, and that subprocess can do arbitrary things
👍 1
no different from how a shell launches a program you type at its prompt as a POSIX subprocess
💯 2
j
Since the kernel is getting the target from the chroot, then changes to the repo won't impact a running kernel, correct? This is actually a small pain point in that engineers have to quit and restart notebooks when they change python libraries. 🤷🏽‍♀️
Sweet.
InteractiveProcess
sounds like what I want. And
Repl
uses that under the hood.
h
That’s correct that it will not restart the notebook if the code changes. It will continue using the chroot’s code, rather than the repo’s changed code, afaict. Is that the behavior you want?
It’s not working robustly, but I think the idea is that
./pants --loop
will allow you to restart the notebook upon changes
j
I don't think we want that at this point.
If a DS eng is doing some large number crunching that takes a few hours and makes a change to a file while it is running they could potentially restart the whole job. That would be sad.
👍 1