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