12

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?

0

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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
4
+50
% 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

3 Comments

kernel-21247 is the kernel name or the .json file name?
@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.
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?
4

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.

1 Comment

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 // my answer using Python API does not open a new console though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.