I can run a python script from a bash shell like so:
>> python script.py
I can also start an iPython kernel and connect multiple iPython consoles to the same kernel like so:
>> ipython kernel
...
To connect another client to this kernel, use:
--existing kernel-8987.json
then, for as many consoles as I would like, I execute
>> jupyter console --existing kernel-8987.json
However, what I would like to do is start a kernel, but then run scripts without opening a console. I'd like to do something like this:
>> ipython --existing kernel-8987.json script.py
Is this possible to do this somehow?
You can use run command in the input prompt to run a Python script. The run command is actually line magic command and should actually be written as %run. However, the %automagic mode is always on by default, so you can omit this.
Extending on the other answer and use the %run
magic command1, it's possible to do this (which works for multiple-line scripts):
$ jupyter console --simple-prompt --existing kernel-8987.json <<< '%run script.py'
or (on Windows where <<<
doesn't work)
> echo %run script.py | jupyter console --simple-prompt --existing kernel-8987.json
Unfortunately, this method still prints some redundant prompt (In[1]:
) on the console.
Alternatively, using Python API: (create a script to execute this Python code) (on my machine, this method is much faster)
from jupyter_client.manager import KernelManager
manager=KernelManager(connection_file="full path to connection file.json")
manager.load_connection_file()
manager.client().execute("commands to execute")
The commands to execute might span multiple lines, or have the form %run file.py
.
There's also silent=True, store_history=False
as parameters to execute()
.
The full path to the connection file can be found as
Path(jupyter_core.paths.jupyter_runtime_dir()) / "kernel-8987.json"
, orjupyter_client.connect.find_connection_file("kernel-8987.json")
(find file with specific name), orjupyter_client.connect.find_connection_file()
(find the latest file)See also jupyter-client
documentation.
Disclaimer: Some of the code are taken from jupyter-vim plugin, and manager.load_connection_file
and connect.find_connection_file
appears to be undocumented.
1: As mentioned in the documentation, the file is executed in a new namespace unless you use -i
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With