Skip to content

Commit

Permalink
chore: konsole
Browse files Browse the repository at this point in the history
  • Loading branch information
echarles committed Dec 12, 2024
1 parent fcfd84c commit 6e8ae24
Showing 5 changed files with 63 additions and 45 deletions.
54 changes: 29 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -32,31 +32,38 @@ pip install jupyter_kernel_client

### Kernel Client

1. Start a jupyter server (or JupyterLab or Jupyter Notebook)
1. Start a Jupyter Server (or JupyterLab or Jupyter Notebook)

```sh
jupyter server
# ...
# To access the server, open this file in a browser:
# file:///home/echarles/.local/share/jupyter/runtime/jpserver-910631-open.html
# Or copy and paste one of these URLs:
# http://localhost:8888/?token=aed1fef59f754b9bfc99017e1dcf4d5602fc1a97d331069b
# http://127.0.0.1:8888/?token=aed1fef59f754b9bfc99017e1dcf4d5602fc1a97d331069b
```

2. Note down the URL (usually `http://localhost:8888`) and the server token
2. Note down the URL (usually `http://localhost:8888`) and the Server Token (in the above example it will be `aed1fef59f754b9bfc99017e1dcf4d5602fc1a97d331069b`)

3. Open a Python terminal
3. Launch `python` in a terminal.

4. Execute the following snippet

```py
import os

from platform import node
from jupyter_kernel_client import KernelClient

with KernelClient(server_url="http://localhost:8888", token="...") as kernel:

with KernelClient(server_url="http://localhost:8888", token="aed1fef59f754b9bfc99017e1dcf4d5602fc1a97d331069b") as kernel:
reply = kernel.execute(
"""import os
from platform import node
print(f"Hey {os.environ.get('USER', 'John Smith')} from {node()}.")
"""
)

assert reply["execution_count"] == 1
assert reply["outputs"] == [
{
@@ -72,31 +79,37 @@ print(f"Hey {os.environ.get('USER', 'John Smith')} from {node()}.")

This package can be used to open a Jupyter Console to a Jupyter Kernel through HTTP 🐣.

1. Install the optional dependencies:
1. Install the optional dependencies.

```sh
pip install jupyter-kernel-client[console]
pip install jupyter-kernel-client[konsole]
```


2. Start a jupyter server (or JupyterLab or Jupyter Notebook)
2. Start a Jupyter Server (or JupyterLab or Jupyter Notebook).

```sh
jupyter server
# ...
# To access the server, open this file in a browser:
# file:///home/echarles/.local/share/jupyter/runtime/jpserver-910631-open.html
# Or copy and paste one of these URLs:
# http://localhost:8888/?token=aed1fef59f754b9bfc99017e1dcf4d5602fc1a97d331069b
# http://127.0.0.1:8888/?token=aed1fef59f754b9bfc99017e1dcf4d5602fc1a97d331069b
```

3. Note down the URL (usually `http://localhost:8888`) and the server token
4. Start the console
3. Note down the URL (usually `http://localhost:8888`) and the Server Token (in the above example it will be `aed1fef59f754b9bfc99017e1dcf4d5602fc1a97d331069b`)

4. Start the console.

```sh
jupyter-connect --url http://localhost:8888 --token abcdef...
jupyter-konsole --url http://localhost:8888 --token aed1fef59f754b9bfc99017e1dcf4d5602fc1a97d331069b
```

Example of console session:
Example of console session.

```
❯ jupyter-connect --token 0d2004a3f836e3dbb01a035c66a43b6fa06e44b004599835
[ConsoleApp] KernelHttpManager created a new kernel: ...
```bash
❯ jupyter-konsole --url http://localhost:8888 --token 0d2004a3f836e3dbb01a035c66a43b6fa06e44b004599835
[KonsoleApp] KernelHttpManager created a new kernel: ...
Jupyter Kernel console 0.2.0

Python 3.12.7 | packaged by conda-forge | (main, Oct 4 2024, 16:05:46) [GCC 13.3.0]
@@ -117,15 +130,6 @@ To remove the extension, execute:
pip uninstall jupyter_kernel_client
```

## Troubleshoot

If you are seeing the frontend extension, but it is not working, check
that the server extension is enabled:

```bash
jupyter server extension list
```

## Contributing

### Development install
9 changes: 7 additions & 2 deletions jupyter_kernel_client/__init__.py
Original file line number Diff line number Diff line change
@@ -5,9 +5,14 @@
"""Jupyter Kernel Client through websocket."""

from .client import KernelClient
from .konsoleapp import KonsoleApp
from .manager import KernelHttpManager
from .wsclient import KernelWebSocketClient

__version__ = "0.3.1"

__all__ = ["KernelClient", "KernelHttpManager", "KernelWebSocketClient"]
__all__ = [
"KernelClient",
"KernelHttpManager",
"KernelWebSocketClient",
"KonsoleApp",
]
8 changes: 8 additions & 0 deletions jupyter_kernel_client/__version__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) 2023-2024 Datalayer, Inc.
#
# BSD 3-Clause License

"""Jupyter Kernel Client through websocket."""


__version__ = "0.3.1"
Original file line number Diff line number Diff line change
@@ -16,16 +16,17 @@
from .manager import KernelHttpManager
from .shell import WSTerminalInteractiveShell


# -----------------------------------------------------------------------------
# Globals
# -----------------------------------------------------------------------------

_examples = """
# Start a console connected to a local Jupyter Server running at http://localhost:8888 with a new python kernel.
jupyter connect --token <server_token>
jupyter konsole --token <server_token>
# Start a console connected to a distant Jupyter Server with a new python kernel.
jupyter connect --url https://my.jupyter-server.xzy --token <server_token>
jupyter konsole --url https://my.jupyter-server.xzy --token <server_token>
"""

# -----------------------------------------------------------------------------
@@ -37,16 +38,16 @@
flags.update(
boolean_flag(
"confirm-exit",
"ConsoleApp.confirm_exit",
"KonsoleApp.confirm_exit",
"""Set to display confirmation dialog on exit. You can always use 'exit' or
'quit', to force a direct exit without any confirmation. This can also
be set in the config file by setting
`c.ConsoleApp.confirm_exit`.
`c.KonsoleApp.confirm_exit`.
""",
"""Don't prompt the user when exiting. This will terminate the kernel
if it is owned by the frontend, and leave it alive if it is external.
This can also be set in the config file by setting
`c.ConsoleApp.confirm_exit`.
`c.KonsoleApp.confirm_exit`.
""",
)
)
@@ -63,10 +64,10 @@
aliases = dict(base_aliases)
aliases.update(
{
"existing": "ConsoleApp.existing",
"kernel": "ConsoleApp.kernel_name",
"token": "ConsoleApp.token",
"url": "ConsoleApp.server_url",
"existing": "KonsoleApp.existing",
"kernel": "KonsoleApp.kernel_name",
"token": "KonsoleApp.token",
"url": "KonsoleApp.server_url",
}
)

@@ -75,10 +76,10 @@
# -----------------------------------------------------------------------------


class ConsoleApp(JupyterApp):
class KonsoleApp(JupyterApp):
"""Start a terminal frontend to a kernel."""

name = "jupyter-connect"
name = "jupyter-konsole"
version = __version__

description = """
@@ -93,7 +94,7 @@ class ConsoleApp(JupyterApp):
single-process Terminal IPython shell, such as connecting to an
existing jupyter kernel, via:
jupyter connect --token <server token> --existing <kernel_id>
jupyter konsole --token <server token> --existing <kernel_id>
where the previous session could have been created by another jupyter
console, or by opening a notebook.
@@ -218,7 +219,7 @@ def start(self):
self.kernel_client.client.stop_channels()


main = launch_new_instance = ConsoleApp.launch_instance
main = launch_new_instance = KonsoleApp.launch_instance


if __name__ == "__main__":
10 changes: 5 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -23,22 +23,22 @@ classifiers = [
dependencies = ["jupyter_core", "jupyter_client>=7.4.4", "requests", "traitlets>=5.3", "websocket-client"]

[project.optional-dependencies]
console = ["jupyter_console"]
konsole = ["jupyter_console"]
test = ["ipykernel", "jupyter_server>=1.6,<3", "pytest>=7.0"]
lint = ["mdformat>0.7", "mdformat-gfm>=0.3.5", "ruff"]
typing = ["mypy>=0.990"]

[project.scripts]
jupyter-konsole = "jupyter_kernel_client.konsoleapp:main"

[project.license]
file = "LICENSE"

[project.scripts]
jupyter-connect = "jupyter_kernel_client.consoleapp:main"

[project.urls]
Home = "https://github.com/datalayer/jupyter-kernel-client"

[tool.hatch.version]
path = "jupyter_kernel_client/__init__.py"
path = "jupyter_kernel_client/__version__.py"

[tool.pytest.ini_options]
filterwarnings = [

0 comments on commit 6e8ae24

Please sign in to comment.