10

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?

mrclary
  • 489
  • 3
  • 16

3 Answers3

3

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", or
  • jupyter_client.connect.find_connection_file("kernel-8987.json") (find file with specific name), or
  • jupyter_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.

user202729
  • 3,358
  • 3
  • 25
  • 36
  • 1
    You can use `connection_file = jupyter_client.connect.find_connection_file()` if you don't want to provide a path to the kernel file and would like your script to work as if `--existing` has been passed in the command line – overflo Sep 29 '21 at 11:42
2

It seems that jupyter has this functionality already built in.

$ jupyter run --existing kernel-8987.json script.py

I don't know if this was a feature addition since the OP, or if I was really stupid and didn't RTFM.

$ jupyter run --help
Run Jupyter kernel code.

Options
=======
...
--existing=<CUnicode>
    Connect to an already running kernel
    Default: ''
    Equivalent to: [--JupyterConsoleApp.existing]

The other answers will run the code in the existing kernel, but still open a console in the process. The run subcommand does exactly what I requested: runs the code without opening an interactive console.

mrclary
  • 489
  • 3
  • 16
  • 1
    Looks like `jupyter run` was first added in Aug 8, 2016 in version 5.0.0. [Add jupyter-run command to jupyter_client collection · jupyter/jupyter_client@08d6c30](https://github.com/jupyter/jupyter_client/commit/08d6c300dbd08f0ef14ea1bb62608c84ee2504a4) // my answer using Python API does not open a new console though. – user202729 Aug 05 '22 at 11:04
1
% jupyter console --version
6.2.0

I can exec one-liners from a file like this:

% jupyter console --simple-prompt --existing kernel-8987.json < script.py
paiv
  • 5,491
  • 22
  • 30
  • kernel-21247 is the kernel name or the .json file name? – Arroz con Tomate Mar 15 '21 at 08:37
  • @ArrozconTomate the json file is the moniker for the kernel, as described in the question. I've update to show where the `script.py` goes. But still note it would only accept complete one line statements, not nested blocks. At least not in the version I've tested. – paiv Mar 15 '21 at 12:38
  • How did you get the .json file? Simply by using 'jupyter kernelspec list' and browsing in the corresponding file location? or is there another way to do it? – Arroz con Tomate Mar 16 '21 at 14:16