An iPython kernel for Nion Swift.
Project description
Introduction
A plugin for Nion Swift that implements an ipython kernel.
It implements the most important messages of the jupyter/ipython messaging protocol.
Messages of the clients are received on the shell channel. These messages are processed by MessageHandlers and each message type has its own handler that only processes this message type. New handlers need to inherit from the base MessageHandler class in nion.ipython_kernel.ipython_kernel.py and the handler instance need to be registered with IpythonKernel.register_shell_handler.
The content dictionary of a message matching the handler’s type will be passed to a handler’s process_request method. The process_request method must return a dictionary containing the reply content.
To connect a jupyter console to this kernel, run:
jupyter console --existing nionswift-ipython-kernel.json
In order to connect a jupyter notebook you need to install nionswift-ipython-provisioner in the client environment. This is needed because jupyter notebooks cannot connect to a running kernel by default, so a custom kernel provisioner is required for this.
Matplotlib integration
If matplotlib is installed in the python environment running Swift, this kernel supports inline plotting and in addition it can send plots to Swift as a data item.
Default is inline plotting. To switch between plot styles, you can use the %matplotlib line magic:
%matploltib inline enables inline plotting.
%matplotlib swift enables plotting to Swift data items.
Other matplotlib gui backends can also be used, but be aware that they might cause awkward interactions betweem their event loop and the swift event loop. So it is recommended to only use the two explicitly supported backends listed above. Calling %matplotlib auto will also enable the “inline” backend.
Threading
The downside of directly running an ipython kernel within Swift is that long calculations will freeze the Swift UI for their entire duration. In terms of threading, this ipython kernel behaves exactly like the built-in console in Swift. So just like it is described in the documentation for the built-in console, you can run your code on a background thread to avoid locking up the UI.
Here is a short example for how to run code on a thread:
import threading
import time
def do_something_slow():
time.sleep(10.0)
print('Done doing something slow.')
threading.Thread(target=do_something_slow).start()
This will print "Done doing something slow." after 10 seconds, but will not freeze the Swift UI.
Two important notes:
If you run code on a thread, you can only access objects that are thread-safe, otherwise you may get undefined behavior. As a rule of thumb, any UI access is typically NOT threadsafe. So for example diplaying the result of a processing routine as a new data item cannot be done from a thread.
The threading.Thread(...).start() call returns immediately, since the work is done in the background. That means, if you need to wait for the result for example to diplay it in Swift, you need to wait until the background thread is done before you have access to the result.
The two notes above are the main reason that threading can be a bit tricky to use. If you need to queue a task to be executed after a processing routine running on a thread has finished, you can use api.queue_task(). This will queue the function passed to it for execution on the main thread. So for example showing the result data in a new data item can be accomplished like this:
import threading
import time
import numpy
def do_something_slow():
time.sleep(10.0)
print('Done doing something slow.')
data = numpy.random.rand(16, 32)
def show_result():
data_item = api.library.create_data_item_from_data(data, title='Result')
api.application.document_windows[0].display_data_item(data_item)
api.qeue_task(show_result)
threading.Thread(target=do_something_slow).start()
Note that you must not add the call operator (parenthesis) to the function you are passing to queue_task!
Some more examples for how to interact with the Swift API can be found here.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nionswift_ipython_kernel-0.1.2.tar.gz.
File metadata
- Download URL: nionswift_ipython_kernel-0.1.2.tar.gz
- Upload date:
- Size: 20.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
55c5cf02ce4a7629a831edac9b925e9097f137dae542064bea57e48e5975678c
|
|
| MD5 |
ac00bae9d8d03fb4f837ddb6b247b009
|
|
| BLAKE2b-256 |
bee18910ec4d6cd9b78da2f6f2ea33df2be83da78b9d5b13e6fc54403a9869a6
|
Provenance
The following attestation bundles were made for nionswift_ipython_kernel-0.1.2.tar.gz:
Publisher:
python-package.yml on nion-software/nionswift-ipython-kernel
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nionswift_ipython_kernel-0.1.2.tar.gz -
Subject digest:
55c5cf02ce4a7629a831edac9b925e9097f137dae542064bea57e48e5975678c - Sigstore transparency entry: 1137583015
- Sigstore integration time:
-
Permalink:
nion-software/nionswift-ipython-kernel@94d2237ade7b3d6deb29f2d51ea07c0f2c4176db -
Branch / Tag:
refs/tags/0.1.2 - Owner: https://github.com/nion-software
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-package.yml@94d2237ade7b3d6deb29f2d51ea07c0f2c4176db -
Trigger Event:
push
-
Statement type:
File details
Details for the file nionswift_ipython_kernel-0.1.2-py3-none-any.whl.
File metadata
- Download URL: nionswift_ipython_kernel-0.1.2-py3-none-any.whl
- Upload date:
- Size: 21.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
321cb4a8a9ac461810fb4d4194f55ff8519614ad81cd6894b9de0747eee41c9f
|
|
| MD5 |
8295ea9cf1c8584b3d719d4cf391eb62
|
|
| BLAKE2b-256 |
d69fa77f034687920e3f36a4e776f36267d6dd9c27a13145af81d6cfd78499e7
|
Provenance
The following attestation bundles were made for nionswift_ipython_kernel-0.1.2-py3-none-any.whl:
Publisher:
python-package.yml on nion-software/nionswift-ipython-kernel
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nionswift_ipython_kernel-0.1.2-py3-none-any.whl -
Subject digest:
321cb4a8a9ac461810fb4d4194f55ff8519614ad81cd6894b9de0747eee41c9f - Sigstore transparency entry: 1137583055
- Sigstore integration time:
-
Permalink:
nion-software/nionswift-ipython-kernel@94d2237ade7b3d6deb29f2d51ea07c0f2c4176db -
Branch / Tag:
refs/tags/0.1.2 - Owner: https://github.com/nion-software
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-package.yml@94d2237ade7b3d6deb29f2d51ea07c0f2c4176db -
Trigger Event:
push
-
Statement type: