core

Main(/all) routines for ipyfernel

Setup Routines

The remote kernel needs to be speified by the user. The SSH config will typically be called by other routines since the port is usually changing each time.


source

register_remote_kernel


def register_remote_kernel(
    remote_python:str='/path/to/python', # Full path of Python executable to run on remote system.
    kernel_name:str='ipyf_remote_kernel', # Any old name will do. This is fine.
    display_name:str='Remote Python', # This is just what you'll see when you look at a list.
    ssh_host_alias:str='remote_server_sshpyk', # Same alias as was used in writing to ssh config file.
    remote_kernel_name:str='python3', # Typical for Jupiter.
    language:str='python', # Probably want to leave this unless you want to try R.
    verbose:bool=True, # Print extra info.
):

registers which python kernel will be used on remote machine

register_remote_kernel(remote_python='/Users/shawley/exercises/solveit/.venv/bin/python')
ipyf_remote_kernel is already a registered kernel

source

set_ssh_config


def set_ssh_config(
    port:int, # Port number on proxy server (e.g. bore.pub)
    user:str='', # Username on remote system.
    alias:str='remote_server_sshpyk', # Default alias for `sshpyk`, leave it alone
    proxyname:str='bore.pub', # Have tested this with bore
    config_path:str='~/.ssh/config', # Shouldn't need to change this.
):

(called by ipf_startup) sets up user’s ssh .config file with info used later

# Demo that:
# On remote machine run:  bore local 22 --to bore.pub
# Take that port and put it here
port = 3365
set_ssh_config(port)
/app/data/.ssh/config file updated.

Lower-Level Routines

These back-end routines typically don’t need to be called directly by the user; rather they’re called by the user interface routines.


source

ipf_startup


def ipf_startup(
    kernel_name:str='ipyf_remote_kernel'
):

Start up the remote kernel

ipf_startup()

source

ipf_exec


def ipf_exec(
    code:str, # Code to be executed
    verbose:bool=False, # Return details about remote execution.
):

Execute code on the remote kernel.

code = """
import platform 
print(platform.system())
"""
ipf_exec(code)

source

ipf_shutdown


def ipf_shutdown(
    verbose:bool=True
):

Terminates the remote kernel

ipf_shutdown()

source

connect_existing_kernel


def connect_existing_kernel(
    tunnel, kernel_name:str='ipyf_remote_kernel'
):

Connect to existing remote kernel

User Interface Routines

These are the main routines you’ll typically use:

  1. Start and stop remote the remote kernel
  2. iPython magics %%remote and %%local
  3. (optional) “Sticky” mode, to redirect cell execution.

Start and Stop Remote Kernel


source

start_remote


def start_remote(
    port, user:str=''
):

Configure ssh connection to remote server and start remote server

start_remote(port)

source

stop_remote


def stop_remote(
    
):

shutdown remote server

iPython Magics

These line or cell magics, invoked via ‘%’ or ‘%%’ respectively, will direct execution of a line or cell to occur on the appropriate system – “remote” or “local”, where local means in the current notebook.


source

remote


def remote(
    line, cell:NoneType=None
):

remote exeuction: works as %remote and as %%remote

#%%remote    <-- docs are filtering magics but that's what's used here
import socket 
hostname = socket.gethostname()   # let's make sure we're running remotely
print("Hello from",hostname)
Hello from 41bcdf3ad182

^ “Chonk” is the name of my home laptop


source

local


def local(
    line, cell:NoneType=None
):

local execution: works as %local and as %%local

#%%local <-- docs are filtering magics but that's what's used here
import socket 
hostname = socket.gethostname()   # let's make sure we're running remotely
print("Hello from",hostname)
Hello from c54fa983f332

^“c54fa983f332” happens to be the name of the solvent instance currently running.

‘Sticky’/‘Seamless’ Remote Excution

via Input Transformers. These can make cells set execute remotely by default.

WARNINGS: 1. Solve it is not intended to work with people modifying input transformers So be wary. Nevertheless, this seems to work. 2. If they’re commands that you definitely want to execute locally, maybe run %unset_sticky just to be sure first.


source

set_sticky


def set_sticky(
    
):

Makes code cells execute remotely, via input transformer


source

unset_sticky


def unset_sticky(
    
):

Un-sticks remote execution for code cells

stop_remote()

print()
start_remote(port)
set_sticky()
# remote execution
import socket 
hostname = socket.gethostname()
print("Hello from",hostname)
Hello from c54fa983f332
unset_sticky()
Code cells will now run locally.
# local execution
import socket 
hostname = socket.gethostname()
print("Hello from",hostname)
Hello from c54fa983f332