Skip to main content

Execute Python .py files on remote Jupyter servers

Project description

pyrun-jupyter

Execute Python .py files on remote Jupyter servers.

PyPI version Python 3.8+ License: MIT GitHub

Installation

pip install pyrun-jupyter

Quick Start

from pyrun_jupyter import JupyterRunner

# Connect to your Jupyter server
runner = JupyterRunner("http://localhost:8888", token="your_token")

# Run a .py file on the remote server
result = runner.run_file("script.py")
print(result.stdout)

# Or run code directly
result = runner.run("print('Hello from Jupyter!')")
print(result.stdout)

Features

  • 🐍 Execute standard .py files on remote Jupyter kernels
  • 📤 Pass parameters to your scripts
  • 📥 Capture stdout, stderr, and rich outputs
  • 🔄 Kernel management (start, stop, restart)
  • 🔌 Connect to existing kernels
  • ⚡ Context manager support for automatic cleanup

Usage Examples

Basic Usage

from pyrun_jupyter import JupyterRunner

runner = JupyterRunner("http://jupyter-server:8888", token="xxx")

# Execute code
result = runner.run("x = 42; print(f'The answer is {x}')")
print(result.stdout)  # The answer is 42

# Clean up
runner.stop_kernel()

Using Context Manager (Recommended)

from pyrun_jupyter import JupyterRunner

with JupyterRunner("http://localhost:8888", token="xxx") as runner:
    result = runner.run_file("my_script.py")
    print(result.stdout)
# Kernel automatically stopped

Passing Parameters to Scripts

from pyrun_jupyter import JupyterRunner

with JupyterRunner("http://localhost:8888", token="xxx") as runner:
    # Parameters are injected as variables in your script
    result = runner.run_file(
        "train_model.py",
        params={
            "learning_rate": 0.001,
            "epochs": 100,
            "batch_size": 32,
        }
    )
    print(result.stdout)

Your train_model.py can use these variables directly:

# train_model.py
print(f"Training with lr={learning_rate}, epochs={epochs}")
# ... your training code

Handling Errors

from pyrun_jupyter import JupyterRunner, ExecutionError

runner = JupyterRunner("http://localhost:8888", token="xxx")

result = runner.run("1/0")
if result.has_error:
    print(f"Error: {result.error_name}: {result.error}")
    # Error: ZeroDivisionError: division by zero

Connecting to Existing Kernel

runner = JupyterRunner("http://localhost:8888", token="xxx", auto_start_kernel=False)

# List available kernels
kernels = runner.list_kernels()
print(kernels)

# Connect to a specific kernel
runner.connect_to_kernel("existing-kernel-id")
result = runner.run("print('Using existing kernel!')")

Managing Kernels

runner = JupyterRunner("http://localhost:8888", token="xxx")

# Start a specific kernel type
runner.start_kernel("python3")

# Restart kernel (clears state)
runner.restart_kernel()

# Stop kernel when done
runner.stop_kernel()

ExecutionResult

The run() and run_file() methods return an ExecutionResult object:

Attribute Type Description
stdout str Standard output
stderr str Standard error
success bool Whether execution succeeded
error str Error message (if failed)
error_name str Exception type (e.g., 'ValueError')
error_traceback list Full traceback
data dict Rich output (text/plain, text/html, etc.)
execution_count int Jupyter cell execution count

Configuration

Parameter Default Description
url (required) Jupyter server URL
token None Authentication token
kernel_name "python3" Kernel specification to use
auto_start_kernel True Start kernel automatically on first run

Getting Your Jupyter Token

From Jupyter Notebook/Lab

When you start Jupyter, it displays a URL with the token:

http://localhost:8888/?token=abc123...

Generate a permanent token

jupyter server --generate-config
# Edit ~/.jupyter/jupyter_server_config.py
# Set: c.ServerApp.token = 'your-secret-token'

Requirements

  • Python >= 3.8
  • A running Jupyter server (Notebook, Lab, or Hub)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyrun_jupyter-0.3.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyrun_jupyter-0.3.0-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file pyrun_jupyter-0.3.0.tar.gz.

File metadata

  • Download URL: pyrun_jupyter-0.3.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyrun_jupyter-0.3.0.tar.gz
Algorithm Hash digest
SHA256 2c391661af9cfc67ea0764d1cf6a7ddd5dc5de64e9a08beca4d645523f3c57ff
MD5 52ea07e4e41687ef3208d0c5d1faa058
BLAKE2b-256 e1ebb0172569e7df67ae40d76d64b405fa61b9466a70519d94047ae89824ed8b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrun_jupyter-0.3.0.tar.gz:

Publisher: publish.yml on petitoff/pyrun-jupyter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyrun_jupyter-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pyrun_jupyter-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyrun_jupyter-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dfd9badbcdbb1e7aec77bf6840225aead41ca58ddd799e65c4a07d84de7e11c9
MD5 78d2b660015b9b9e28130c3b73ca7df2
BLAKE2b-256 d3ab0b22df141ba7580e552c4022f5ff3097ab276a734d9b94189ef9ee67c356

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrun_jupyter-0.3.0-py3-none-any.whl:

Publisher: publish.yml on petitoff/pyrun-jupyter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page