In [ ]:
Copied!
# MIT License
#
# Copyright (c) <year> MkDocs4 Documentation Team
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.16.1
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# MIT License
#
# Copyright (c) MkDocs4 Documentation Team
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.16.1
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
Python Script Notebook Example¶
This notebook is written as a Python script using Jupytext format. It demonstrates how .nb.py files can be used with mkdocs-jupyter.
import matplotlib.pyplot as plt
In [ ]:
Copied!
# Import required libraries
import numpy as np
print("This is a Jupytext notebook!")
# Import required libraries
import numpy as np
print("This is a Jupytext notebook!")
Creating a Simple Plot¶
Let's create a simple sine wave plot:
In [ ]:
Copied!
# Generate data
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
# Create the plot
plt.figure(figsize=(8, 4))
plt.plot(x, y, "b-", linewidth=2)
plt.title("Sine Wave")
plt.xlabel("x")
plt.ylabel("sin(x)")
plt.grid(True, alpha=0.3)
plt.show()
# Generate data
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
# Create the plot
plt.figure(figsize=(8, 4))
plt.plot(x, y, "b-", linewidth=2)
plt.title("Sine Wave")
plt.xlabel("x")
plt.ylabel("sin(x)")
plt.grid(True, alpha=0.3)
plt.show()
Benefits of Jupytext¶
Using Jupytext format (.nb.py) has several advantages:
- Version Control: Python scripts are much easier to diff and merge
- IDE Support: Full Python IDE features (autocomplete, refactoring)
- Collaboration: Easier for team members to review and edit
- Lightweight: No binary data or output stored in the file
In [ ]:
Copied!
# Example of code that would be easier to review in .py format
def fibonacci(n):
"""Generate Fibonacci sequence up to n terms."""
if n <= 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
else:
fib = [0, 1]
for i in range(2, n):
fib.append(fib[i - 1] + fib[i - 2])
return fib
# Generate and display first 10 Fibonacci numbers
fib_sequence = fibonacci(10)
print(f"First 10 Fibonacci numbers: {fib_sequence}")
# Example of code that would be easier to review in .py format
def fibonacci(n):
"""Generate Fibonacci sequence up to n terms."""
if n <= 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
else:
fib = [0, 1]
for i in range(2, n):
fib.append(fib[i - 1] + fib[i - 2])
return fib
# Generate and display first 10 Fibonacci numbers
fib_sequence = fibonacci(10)
print(f"First 10 Fibonacci numbers: {fib_sequence}")