Jupyter Notebooks
Download ready-to-use Jupyter notebooks from the Kanva dashboard.
Downloading Notebooks
Each trained model in Kanva can generate customized Jupyter notebooks:
- Open your project in the Kanva dashboard
- Navigate to the Use Model section
- Select API tab
- 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
-
Install JupyterLab:
pip install jupyterlab -
Start the server:
jupyter lab -
Open the downloaded
.ipynbfile -
Update the
API_KEYvariable with your actual key -
Run cells with
Shift+Enter
Running in Google Colab
- Go to colab.research.google.com
- File > Upload notebook
- Select your downloaded
.ipynbfile - 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