{"library":"nbparameterise","title":"nbparameterise Library","description":"nbparameterise is a Python library (current version 0.6.1) designed to re-run Jupyter notebooks while substituting input parameters in the first cell. It's often used for automating notebook execution with different configurations or datasets, making it a valuable tool for reproducible research and data pipeline orchestration. The project maintains a steady release cadence, typically driven by bug fixes and minor feature enhancements.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install nbparameterise"],"cli":null},"imports":["from nbparameterise import parameterise","from nbparameterise import execute_notebook"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import os\nfrom nbparameterise import parameterise, execute_notebook\nimport nbformat\n\n# 1. Define the input notebook content (as a string) that includes parameters\ninput_notebook_content = \"\"\"\n{\n \"cells\": [\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": null,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"# Parameters\\n\",\n    \"x = 10\\n\",\n    \"y = 'hello'\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": null,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"print(f'Original x: {x}')\\n\",\n    \"print(f'Original y: {y}')\\n\",\n    \"result = x * 2\\n\",\n    \"print(f'Result: {result}')\"\n   ]\n  }\n ],\n \"metadata\": {\n  \"kernelspec\": {\n   \"display_name\": \"Python 3\",\n   \"language\": \"python\",\n   \"name\": \"python3\"\n  },\n  \"language_info\": {\n   \"codemirror_mode\": {\n    \"name\": \"ipython\",\n    \"version\": 3\n   },\n   \"file_extension\": \".py\",\n   \"mimetype\": \"text/x-python\",\n   \"name\": \"python\",\n   \"nbconvert_exporter\": \"python\",\n   \"pygments_lexer\": \"ipython3\",\n   \"version\": \"3.x.x\"\n  }\n },\n \"nbformat\": 4,\n \"nbformat_minor\": 4\n}\n\"\"\"\n# Define file paths\ninput_path = \"quickstart_input.ipynb\"\noutput_path = \"quickstart_output.ipynb\"\n\n# Save the dummy input notebook file\nwith open(input_path, \"w\") as f:\n    f.write(input_notebook_content)\nprint(f\"Created input notebook: {input_path}\")\n\ntry:\n    # Load the notebook object\n    with open(input_path) as f:\n        nb = nbformat.read(f, as_version=4)\n\n    # Extract current parameters and define new values to override them\n    params = parameterise.extract_parameters(nb)\n    new_params = parameterise.parameter_values(params, x=25, y=\"world!\") # Override x and y\n    parameterised_nb = parameterise.replace_definitions(nb, new_params)\n\n    # Execute the modified notebook and save the output\n    # Requires 'ipykernel' to be installed in the environment.\n    execute_notebook(parameterised_nb, output_path)\n    print(f\"Notebook executed and saved to {output_path}\")\n\n    # Optionally, verify the output from the executed notebook\n    with open(output_path) as f:\n        output_nb = nbformat.read(f, as_version=4)\n        print(\"\\n--- Output Notebook Cells ---\")\n        for cell in output_nb.cells:\n            if cell.cell_type == 'code':\n                for output in cell.outputs:\n                    if output.output_type == 'stream':\n                        print(output.text.strip())\n\nexcept Exception as e:\n    print(f\"Error during quickstart execution: {e}\")\n    print(\"Hint: Ensure 'ipykernel' is installed (`pip install ipykernel`) for notebook execution.\")\nfinally:\n    # Clean up generated files\n    if os.path.exists(input_path):\n        os.remove(input_path)\n    if os.path.exists(output_path):\n        os.remove(output_path)\n    print(\"\\nCleaned up generated files.\")","lang":"python","description":"This quickstart demonstrates how to programmatically define, parameterise, and execute a Jupyter notebook using `nbparameterise`. It creates a dummy notebook, overrides its initial parameters, executes it, and then prints the output, finally cleaning up generated files. Ensure `ipykernel` is installed for successful execution.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}