# core


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

# 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.

------------------------------------------------------------------------

<a
href="https://github.com/drscotthawley/ipyfernel/blob/main/ipyfernel/core.py#L20"
target="_blank" style="float:right; font-size:smaller">source</a>

### register_remote_kernel

``` python

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*

``` python
register_remote_kernel(remote_python='/Users/shawley/exercises/solveit/.venv/bin/python')
```

    ipyf_remote_kernel is already a registered kernel

------------------------------------------------------------------------

<a
href="https://github.com/drscotthawley/ipyfernel/blob/main/ipyfernel/core.py#L44"
target="_blank" style="float:right; font-size:smaller">source</a>

### set_ssh_config

``` python

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*

``` python
# 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.

------------------------------------------------------------------------

<a
href="https://github.com/drscotthawley/ipyfernel/blob/main/ipyfernel/core.py#L87"
target="_blank" style="float:right; font-size:smaller">source</a>

### ipf_startup

``` python

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

```

*Start up the remote kernel*

``` python
ipf_startup()
```

------------------------------------------------------------------------

<a
href="https://github.com/drscotthawley/ipyfernel/blob/main/ipyfernel/core.py#L145"
target="_blank" style="float:right; font-size:smaller">source</a>

### ipf_exec

``` python

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

```

*Execute code on the remote kernel.*

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

------------------------------------------------------------------------

<a
href="https://github.com/drscotthawley/ipyfernel/blob/main/ipyfernel/core.py#L156"
target="_blank" style="float:right; font-size:smaller">source</a>

### ipf_shutdown

``` python

def ipf_shutdown(
    verbose:bool=True
):

```

*Terminates the remote kernel*

``` python
ipf_shutdown()
```

------------------------------------------------------------------------

<a
href="https://github.com/drscotthawley/ipyfernel/blob/main/ipyfernel/core.py#L197"
target="_blank" style="float:right; font-size:smaller">source</a>

### connect_existing_kernel

``` python

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

------------------------------------------------------------------------

<a
href="https://github.com/drscotthawley/ipyfernel/blob/main/ipyfernel/core.py#L217"
target="_blank" style="float:right; font-size:smaller">source</a>

### start_remote

``` python

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

```

*Configure ssh connection to remote server and start remote server*

``` python
start_remote(port)
```

------------------------------------------------------------------------

<a
href="https://github.com/drscotthawley/ipyfernel/blob/main/ipyfernel/core.py#L227"
target="_blank" style="float:right; font-size:smaller">source</a>

### stop_remote

``` python

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.

------------------------------------------------------------------------

<a
href="https://github.com/drscotthawley/ipyfernel/blob/main/ipyfernel/core.py#L250"
target="_blank" style="float:right; font-size:smaller">source</a>

### remote

``` python

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

```

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

``` python
#%%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

------------------------------------------------------------------------

<a
href="https://github.com/drscotthawley/ipyfernel/blob/main/ipyfernel/core.py#L256"
target="_blank" style="float:right; font-size:smaller">source</a>

### local

``` python

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

```

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

``` python
#%%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.

------------------------------------------------------------------------

<a
href="https://github.com/drscotthawley/ipyfernel/blob/main/ipyfernel/core.py#L265"
target="_blank" style="float:right; font-size:smaller">source</a>

### set_sticky

``` python

def set_sticky(
    
):

```

*Makes code cells execute remotely, via input transformer*

------------------------------------------------------------------------

<a
href="https://github.com/drscotthawley/ipyfernel/blob/main/ipyfernel/core.py#L276"
target="_blank" style="float:right; font-size:smaller">source</a>

### unset_sticky

``` python

def unset_sticky(
    
):

```

*Un-sticks remote execution for code cells*

``` python
stop_remote()

print()
start_remote(port)
set_sticky()
```

``` python
# remote execution
import socket 
hostname = socket.gethostname()
print("Hello from",hostname)
```

    Hello from c54fa983f332

``` python
unset_sticky()
```

    Code cells will now run locally.

``` python
# local execution
import socket 
hostname = socket.gethostname()
print("Hello from",hostname)
```

    Hello from c54fa983f332
