⚡ Multi-Environment Strategy for Jupyter AI
Deploying Jupyter AI (`jupyter-ai`) across multiple software development life cycle (SDLC) stages requires tailored configurations. While Development environments prioritize interactive experimentation and local LLM tokens, Testing and Production focus on security isolation, pinned dependency locks, and headless server automation. Furthermore, CI/CD and Jenkins pipelines require automated headless execution and notebook validation.
1. Development Environment: Local Interactive Setup & Hot Reloading
Configuring a local developer workstation for rapid interactive experimentation with JupyterLab, `jupyter-ai`, and developer LLM keys.
Bash (setup_dev.sh)
# 1. Create Conda environment for local dev
conda create -n jupyter-dev python=3.11 -y
conda activate jupyter-dev
# 2. Install JupyterLab and jupyter-ai in editable/full mode
pip install jupyterlab "jupyter-ai[all]" python-dotenv
# 3. Configure local environment variables (.env)
echo "OPENAI_API_KEY=sk-dev-placeholder-key" > .env
echo "JUPYTER_TOKEN=dev_secret_token_123" >> .env
# 4. Launch JupyterLab with persistent token
jupyter lab --IdentityProvider.token='dev_secret_token_123' --port=8888
Dev Status: JupyterLab active at http://localhost:8888. Fully interactive with live chat sidebar and `%ai` magic commands [cite: 1.1.2, 1.1.3].
2. Testing Environment: Automated Notebook Validation & Mock LLMs
Configuring a staging/testing environment to validate notebook execution integrity and run unit tests against AI magic commands using mock providers.
Bash & Python (test_environment_setup.sh)
# 1. Create testing virtual environment with strict pins
python -m venv venv-test
source venv-test/bin/activate
pip install --upgrade pip
pip install jupyterlab==4.1.5 jupyter-ai==3.2.0 pytest nbval
# 2. Set mock provider environment variables for isolated unit testing
export OPENAI_API_KEY="sk-test-mock-key"
export JUPYTER_AI_DEFAULT_MODEL="openai:gpt-4o-mini"
# 3. Execute notebook validation via pytest-nbval
pytest --nbval test_notebook.ipynb
Test Status: Notebook execution cells validated successfully. Zero runtime regressions detected in staging pipeline.
3. Production Environment: Secure Headless Server & Enterprise Hardening
Deploying JupyterLab on a secure production cloud server (AWS/GCP/Hostinger) behind Nginx reverse proxy with SSL termination and strict user access controls.
Bash / Systemd Service (jupyter-prod.service)
# /etc/systemd/system/jupyter.service
[Unit]
Description=JupyterLab Production AI Server
After=network.target
[Service]
Type=simple
User=jupyter-prod
WorkingDirectory=/var/www/ai-workspace
ExecStart=/opt/miniconda/envs/ai-prod/bin/jupyter lab --no-browser --port=8888 --ip=127.0.0.1 --ServerApp.token='SECURE_HASHED_TOKEN_XYZ'
Restart=always
[Install]
WantedBy=multi-user.target
Production Status: Server bound to localhost:8888, proxied securely via Nginx with HTTPS and rate-limiting enabled.
4. CI/CD Pipeline: GitHub Actions Automated Notebook Execution
Integrating headless notebook execution and parameterization using Papermill inside GitHub Actions CI/CD build pipelines.
YAML (.github/workflows/jupyter_ci.yml)
name: Jupyter AI CI/CD Pipeline
on:
push:
branches: [ "main" ]
jobs:
execute-notebooks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install Dependencies
run: |
pip install jupyterlab jupyter-ai papermill pytest
- name: Execute Headless Notebook via Papermill
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
run: |
papermill input_analysis.ipynb output_executed.ipynb -p environment "production"
CI/CD Status: Pipeline successfully executed parameterized notebook `input_analysis.ipynb` and archived output artifacts.
5. Jenkins Server: Automated Pipeline Script for Batch AI Processing
Configuring a Jenkins declarative pipeline (`Jenkinsfile`) to provision virtual environments, run headless Jupyter notebooks, and generate compliance reports.
Groovy (Jenkinsfile)
pipeline {
agent any
environment {
OPENAI_API_KEY = credentials('jenkins-openai-secret')
}
stages {
stage('Environment Setup') {
steps {
sh 'python3 -m venv venv-jenkins'
sh './venv-jenkins/bin/pip install --upgrade pip jupyterlab jupyter-ai papermill'
}
}
stage('Run AI Notebook Batch') {
steps {
sh './venv-jenkins/bin/papermill model_training_template.ipynb training_result.ipynb'
}
}
stage('Archive Artifacts') {
steps {
archiveArtifacts artifacts: 'training_result.ipynb', fingerprint: true
}
}
}
}
Jenkins Status: Declarative pipeline completed successfully. Artifact `training_result.ipynb` archived in Jenkins build history.