-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_large_notebook.py
144 lines (124 loc) · 3.59 KB
/
generate_large_notebook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import nbformat
from nbformat.v4 import new_notebook, new_code_cell, new_markdown_cell
# Create a new notebook
nb = new_notebook()
# Specify the default kernel (IPython)
nb.metadata.kernelspec = {
"name": "python3",
"display_name": "Python 3",
"language": "python",
}
# Add the title and Table of Contents (ToC)
nb.cells.append(new_markdown_cell("# Large Test Notebook"))
nb.cells.append(
new_markdown_cell(
"## Table of Contents\n\n"
"- [Papermill](#papermill)\n"
"- [ipywidgets](#ipywidgets)\n"
"- [Dash](#dash)\n"
"- [jupyterlab-lsp](#jupyterlab-lsp)\n"
"- [ipydatagrid](#ipydatagrid)\n"
"- [ipympl](#ipympl)\n"
"- [jupyterlab-code-formatter](#jupyterlab-code-formatter)\n"
"- [dummy-code-cells](#dummy-code-cells)"
)
)
# Define sample code for each extension
extensions = {
"Papermill": """
import tempfile
import papermill as pm
with tempfile.NamedTemporaryFile(delete=False, suffix='.ipynb') as temp_output:
pm.execute_notebook(
"test-papermill.ipynb",
temp_output.name,
parameters={"param1": "TestRun", "param2": 99},
kernel_name="python3"
)
print(f"Output saved to {temp_output.name}")
output_path = temp_output.name
output_path
""",
"ipywidgets": """
import ipywidgets as widgets
from IPython.display import display
slider = widgets.IntSlider(value=50, min=0, max=100, description="Slider:")
button = widgets.Button(description="Click Me")
output = widgets.Output()
def on_button_click(b):
with output:
print("Button Clicked!")
button.on_click(on_button_click)
display(slider, button, output)
""",
"Dash": """
from dash import dcc, html, Dash
app = Dash(__name__)
app.layout = html.Div([
dcc.Graph(
id='graph',
figure={
'data': [{'x': [1, 2, 3, 4], 'y': [10, 11, 12, 13], 'type': 'line', 'name': 'Test'}],
'layout': {'title': 'Dash in JupyterLab'}
}
)
])
app.run_server(host="0.0.0.0", port=8052)
""",
"jupyterlab-lsp": """
import numpy as np
np
""",
"ipydatagrid": """
import numpy as np
import pandas as pd
import ipydatagrid
data = pd.DataFrame(
np.random.randint(0, 100, size=(5, 5)),
columns=["A", "B", "C", "D", "E"]
)
grid = ipydatagrid.DataGrid(data, editable=True)
grid
""",
"ipympl": """
# Activate ipympl
%matplotlib widget
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()
""",
"jupyterlab-code-formatter": """
def my_function(x, y):
print(x, y)
""",
}
# Add one code cell for each extension
for extension_name, code in extensions.items():
nb.cells.append(
new_markdown_cell(
f"## {extension_name}\n\nThis section tests the {extension_name} extension"
)
)
nb.cells.append(new_code_cell(code.strip()))
# Fill with dummy cells until reaching 1000 cells
current_cell_count = len(nb.cells)
dummy_cell_count = 1000 - current_cell_count
extension_codes = list(extensions.values())
nb.cells.append(
new_markdown_cell("## dummy-code-cells\n\nThis section contains filler code cells.")
)
while len(nb.cells) < 1000:
for code in extension_codes:
if len(nb.cells) < 1000:
nb.cells.append(new_code_cell(code))
else:
break
# Write the notebook to a file
notebook_path = "notebooks/test-large-notebook-with-extensions.ipynb"
with open(notebook_path, "w") as f:
nbformat.write(nb, f)
print(f"Large test notebook created: {notebook_path} with {len(nb.cells)} cells.")