# Jupyter Notebook

#### Installation

With pip

```bash
pip install notebook
```

##### Python Virtual Environment

With Python Venv

```bash
mkdir my-rag
cd my-rag
python -m venv .venv
source .venv/bin/activate
(my-rag)> pip install --upgrade pip
(my-rag)> pip install notebook
(my-rag)> jupyter notebook
```

With Conda

```bash
conda create -n my-rag python=3.10
conda activate my-rag
(my-rag)> pip install --upgrade pip
(my-rag)> pip install notebook
(my-rag)> jupyter notebook
```

UI 可切換不同虛擬環境（需要先建立不同的 ipykernel）

```bash
mkdir my-rag
cd my-rag
python -m venv .venv
source .venv/bin/activate
(my-rag)> pip install --upgrade pip
(my-rag)> pip install ipykernel
(my-rag)> ipython kernel install --user --name="my-rag-kernel"
(my-rag)> jupyter notebook
```

#### Tips

##### Secure Authentication

1. In the top menu bar of the notebook editor, select `Add-ons` then `Secrets`.
2. Create a new secret with the label `GOOGLE_API_KEY`.
3. Paste your API key into the "Value" field and click "Save".
4. Ensure that the checkbox next to `GOOGLE_API_KEY` is selected so that the secret is attached to the notebook.

```python
import os
from kaggle_secrets import UserSecretsClient

try:
    GOOGLE_API_KEY = UserSecretsClient().get_secret("GOOGLE_API_KEY")
    os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
    print("✅ Gemini API key setup complete.")
except Exception as e:
    print(
        f"🔑 Authentication Error: Please make sure you have added 'GOOGLE_API_KEY' to your Kaggle secrets. Details: {e}"
    )
```

#### Resources

- [Online Test](https://jupyter.org/try-jupyter/lab/)
- [nbviewer](https://nbviewer.org/) - A simple way to share Jupyter Notebooks

##### CoLab by Google

- [Google Colab](https://colab.research.google.com/) is Jupyter Notebooks that are hosted by Google’s Colaboratory
- [Overview of Colaboratory Features](https://colab.research.google.com/notebooks/basic_features_overview.ipynb)
- [Installing and using Python libraries in Colab](https://colab.research.google.com/notebooks/snippets/importing_libraries.ipynb)
- [Using Google Colab with GitHub](https://colab.research.google.com/github/googlecolab/colabtools/blob/main/notebooks/colab-github-demo.ipynb)
- [Google Colab Tips for Power Users](https://amitness.com/posts/google-colab-tips)