Skip to main content

Jupyter Notebooks

Download ready-to-use Jupyter notebooks from the Kanva dashboard.

Downloading Notebooks

Each trained model in Kanva can generate customized Jupyter notebooks:

  1. Open your project in the Kanva dashboard
  2. Navigate to the Use Model section
  3. Select API tab
  4. Click Download Notebook

The notebook is pre-configured with:

  • Your project ID
  • Example input features matching your model's schema
  • Code for both synchronous and asynchronous predictions

Notebook Contents

Setup Cell

import requests
import time

# Configuration - replace with your actual values
API_URL = "https://kanva.human-driven.ai/api/v1"
API_KEY = "YOUR_API_KEY"
PROJECT_ID = "your-project-id"

Sync Prediction Cell

def predict_sync(input_data: dict, timeout: int = 60) -> dict:
response = requests.post(
f"{API_URL}/projects/{PROJECT_ID}/predict",
headers={"X-API-Key": API_KEY, "Content-Type": "application/json"},
json={"input": input_data},
params={"sync": "true", "timeout": str(timeout)}
)
response.raise_for_status()
return response.json()

# Example usage
result = predict_sync({
"date": ["2024-01-15"],
"category": ["A"],
"value": [123.45]
})

print(f"Prediction: {result['data']['predictions']}")

Async Prediction Cell

def predict_async(input_data: dict, poll_interval: float = 2.0) -> dict:
# Start job
response = requests.post(
f"{API_URL}/projects/{PROJECT_ID}/predict",
headers={"X-API-Key": API_KEY, "Content-Type": "application/json"},
json={"input": input_data}
)
response.raise_for_status()
job_id = response.json()["data"]["jobId"]

# Poll for completion
while True:
status = requests.get(
f"{API_URL}/projects/{PROJECT_ID}/jobs/{job_id}",
headers={"X-API-Key": API_KEY}
).json()

if status["data"]["status"] == "completed":
return status
elif status["data"]["status"] == "failed":
raise Exception(status["data"]["error"])

time.sleep(poll_interval)

Running in JupyterLab

  1. Install JupyterLab:

    pip install jupyterlab
  2. Start the server:

    jupyter lab
  3. Open the downloaded .ipynb file

  4. Update the API_KEY variable with your actual key

  5. Run cells with Shift+Enter

Running in Google Colab

  1. Go to colab.research.google.com
  2. File > Upload notebook
  3. Select your downloaded .ipynb file
  4. Update credentials and run

Security Note

warning

The downloaded notebook contains placeholder credentials. Before sharing or committing notebooks:

  • Remove your actual API key
  • Replace with "YOUR_API_KEY" placeholder