Notebooks¶
Fabric Notebooks are interactive documents that combine code, visualizations, and markdown text. The Notebook API provides access to notebook metadata and content.
Getting a Notebook¶
import fabias
ws = fabias.workspace("Analytics")
# By name
notebook = ws.notebook("ETL Processing")
# By GUID
notebook = ws.notebook("a1b2c3d4-...")
Listing Notebooks¶
ws = fabias.workspace("Analytics")
# List all notebooks
for notebook in ws.notebooks():
print(f"{notebook.name}: {notebook.id}")
Creating Notebooks¶
ws = fabias.workspace("Analytics")
# Create a new notebook
notebook = ws.notebooks.add("Data Exploration")
# Create with description and folder
notebook = ws.notebooks.add(
"Data Exploration",
description="Ad-hoc data exploration and analysis",
folder_id="folder-guid"
)
Notebook Properties¶
notebook = ws.notebook("ETL Processing")
print(f"ID: {notebook.id}")
print(f"Name: {notebook.name}")
print(f"Description: {notebook.description}")
print(f"Type: {notebook.type}")
print(f"Workspace ID: {notebook.workspace_id}")
Getting Notebook Content¶
Notebook content is retrieved via the getDefinition() method, which returns a long-running operation:
notebook = ws.notebook("ETL Processing")
# Fetch notebook definition (async operation)
definition = notebook.getDefinition()
# Access notebook files
for file in definition.files:
print(f"Path: {file.path}")
print(f"Type: {file.type}")
print(f"Content (first 100 chars): {file.content[:100]}")
Notebook Definition Structure¶
The NotebookDefinition object contains:
| Property | Type | Description |
|---|---|---|
files |
List[NotebookFile] | List of notebook files/parts |
Each NotebookFile has:
| Property | Type | Description |
|---|---|---|
path |
str | File path within notebook |
type |
str | Payload type (e.g., "InlineBase64") |
content |
str | Decoded notebook content (UTF-8) |
base64 |
str | Base64-encoded content |
Notebook Formats¶
The format parameter accepts NotebookFormat enum values:
from fabias import NotebookFormat
# GitSource format (default)
definition = notebook.getDefinition(format=NotebookFormat.GITSOURCE)
Cached vs Fresh Load¶
By default, notebook definitions are cached after first retrieval. To force a fresh load:
notebook = ws.notebook("ETL Processing")
# First call - fetches from API
definition1 = notebook.getDefinition()
# Second call - returns cached definition
definition2 = notebook.getDefinition()
# Force fresh load
definition3 = notebook.getDefinition(freshLoad=True)
Updating Notebooks¶
notebook = ws.notebook("ETL Processing")
# Update name and description
notebook.update(
name="ETL Processing v2",
description="Updated ETL pipeline notebook"
)
Deleting Notebooks¶
notebook = ws.notebook("Old Notebook")
# Delete the notebook
notebook.delete()
# Or delete by name/ID via accessor
ws.notebooks.delete("Old Notebook")
Example: Export Notebook Content¶
import fabias
import json
ws = fabias.workspace("Analytics")
notebook = ws.notebook("ETL Processing")
# Get notebook definition
definition = notebook.getDefinition()
# Extract and save content
for file in definition.files:
if file.path.endswith(".ipynb"):
# Parse as JSON if it's a Jupyter notebook
content_dict = json.loads(file.content)
# Save to local file
with open(f"{notebook.name}.ipynb", "w") as f:
json.dump(content_dict, f, indent=2)
print(f"Exported notebook to {notebook.name}.ipynb")
Example: List Notebooks with Metadata¶
import fabias
ws = fabias.workspace("Analytics")
print(f"\nNotebooks in {ws.name}:")
print("-" * 60)
for notebook in ws.notebooks():
print(f"Name: {notebook.name}")
print(f" ID: {notebook.id}")
if notebook.description:
print(f" Description: {notebook.description}")
print()
Example: Search Notebook Content¶
import fabias
def search_notebooks(workspace_name: str, search_term: str):
"""Search for a term in all notebook content."""
ws = fabias.workspace(workspace_name)
print(f"Searching for '{search_term}' in notebooks...")
for notebook in ws.notebooks():
print(f"\nChecking {notebook.name}...")
# Get notebook content
definition = notebook.getDefinition()
for file in definition.files:
if search_term.lower() in file.content.lower():
print(f" ? Found in {notebook.name}")
break
# Usage
search_notebooks("Analytics", "spark.read")
Long-Running Operations¶
The getDefinition() method returns a long-running operation that you can wait for:
notebook = ws.notebook("ETL Processing")
# Start the operation
print("Fetching notebook definition...")
definition = notebook.getDefinition()
# The operation automatically waits and returns the result
print(f"Definition loaded with {len(definition.files)} files")
Refreshing Notebook Data¶
Notebook properties are lazy-loaded and cached. To force a refresh:
notebook = ws.notebook("ETL Processing")
# Make changes via web UI or another client...
# Refresh local data
notebook.refresh()
print(f"Updated name: {notebook.name}")
See Also¶
- Workspaces - Workspace management
- Environments - Notebook environments- Pipelines - Running notebooks in pipelines
- Spark Settings - Spark configuration