Skip to content

Instantly share code, notes, and snippets.

@fomightez
Created March 6, 2025 18:48
Show Gist options
  • Save fomightez/978e1203a4ffbaba37e76efe6ba027af to your computer and use it in GitHub Desktop.
Save fomightez/978e1203a4ffbaba37e76efe6ba027af to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "59fa37ab-7322-4689-b080-383811455164",
"metadata": {},
"source": [
"This is in relation to [this Jupyter Discourse post](https://discourse.jupyter.org/t/how-to-execute-notebook-in-different-namespace/33532?u=fomightez)\n",
"\n",
"-----"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "0e5a96ef-c345-4a8d-88d4-bae54ff368dc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: jupytext in /srv/conda/envs/notebook/lib/python3.10/site-packages (1.16.7)\n",
"Requirement already satisfied: markdown-it-py>=1.0 in /srv/conda/envs/notebook/lib/python3.10/site-packages (from jupytext) (3.0.0)\n",
"Requirement already satisfied: mdit-py-plugins in /srv/conda/envs/notebook/lib/python3.10/site-packages (from jupytext) (0.4.2)\n",
"Requirement already satisfied: nbformat in /srv/conda/envs/notebook/lib/python3.10/site-packages (from jupytext) (5.10.4)\n",
"Requirement already satisfied: packaging in /srv/conda/envs/notebook/lib/python3.10/site-packages (from jupytext) (24.2)\n",
"Requirement already satisfied: pyyaml in /srv/conda/envs/notebook/lib/python3.10/site-packages (from jupytext) (6.0.2)\n",
"Requirement already satisfied: tomli in /srv/conda/envs/notebook/lib/python3.10/site-packages (from jupytext) (2.2.1)\n",
"Requirement already satisfied: mdurl~=0.1 in /srv/conda/envs/notebook/lib/python3.10/site-packages (from markdown-it-py>=1.0->jupytext) (0.1.2)\n",
"Requirement already satisfied: fastjsonschema>=2.15 in /srv/conda/envs/notebook/lib/python3.10/site-packages (from nbformat->jupytext) (2.21.1)\n",
"Requirement already satisfied: jsonschema>=2.6 in /srv/conda/envs/notebook/lib/python3.10/site-packages (from nbformat->jupytext) (4.23.0)\n",
"Requirement already satisfied: jupyter-core!=5.0.*,>=4.12 in /srv/conda/envs/notebook/lib/python3.10/site-packages (from nbformat->jupytext) (5.7.2)\n",
"Requirement already satisfied: traitlets>=5.1 in /srv/conda/envs/notebook/lib/python3.10/site-packages (from nbformat->jupytext) (5.14.3)\n",
"Requirement already satisfied: attrs>=22.2.0 in /srv/conda/envs/notebook/lib/python3.10/site-packages (from jsonschema>=2.6->nbformat->jupytext) (25.1.0)\n",
"Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /srv/conda/envs/notebook/lib/python3.10/site-packages (from jsonschema>=2.6->nbformat->jupytext) (2024.10.1)\n",
"Requirement already satisfied: referencing>=0.28.4 in /srv/conda/envs/notebook/lib/python3.10/site-packages (from jsonschema>=2.6->nbformat->jupytext) (0.36.2)\n",
"Requirement already satisfied: rpds-py>=0.7.1 in /srv/conda/envs/notebook/lib/python3.10/site-packages (from jsonschema>=2.6->nbformat->jupytext) (0.22.3)\n",
"Requirement already satisfied: platformdirs>=2.5 in /srv/conda/envs/notebook/lib/python3.10/site-packages (from jupyter-core!=5.0.*,>=4.12->nbformat->jupytext) (4.3.6)\n",
"Requirement already satisfied: typing-extensions>=4.4.0 in /srv/conda/envs/notebook/lib/python3.10/site-packages (from referencing>=0.28.4->jsonschema>=2.6->nbformat->jupytext) (4.12.2)\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install jupytext"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "62d7330d-7fa7-4ced-8b64-9d456beed87b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"y=100\n",
"Stored 'y' (int)\n"
]
}
],
"source": [
"x = 2\n",
"\n",
"y = 100\n",
"print(f\"{y=}\")\n",
"%store y"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7e12ac02-e53e-4dd9-9612-81128b76e1e1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[jupytext] Reading nbb.ipynb in format ipynb\n",
"[jupytext] Executing notebook with kernel python3\n",
"[jupytext] Writing nbb.ipynb\n"
]
}
],
"source": [
"!jupytext --execute nbb.ipynb"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ef31ffcf-4324-43da-be94-6b9ecfa14556",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"y=100\n"
]
}
],
"source": [
"print(f\"{y=}\")"
]
},
{
"cell_type": "markdown",
"id": "a75ae166-2cae-4784-921b-11da516aabea",
"metadata": {},
"source": [
"`y` in this namespace is still as it was before `nbb.ipynb` was run.\n",
"And if we look at the contents of nbb.ipynb, we can see it was run."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "fd4d3230-ff5e-4c9b-901e-b1eca368d8be",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"y=100\n",
"\n",
"y=150\n",
"\n"
]
}
],
"source": [
"# Show output generated by running `nbb.ipynb` here.\n",
"import nbformat as nbf\n",
"ntbk = nbf.read(\"nbb.ipynb\", nbf.NO_CONVERT)\n",
"text_collected = \"\"\n",
"for cell in ntbk.cells:\n",
" if cell.cell_type == 'code':\n",
" if cell.outputs:\n",
" print(cell.outputs[0]['text'])"
]
},
{
"cell_type": "markdown",
"id": "fd97f49e-28f4-404a-85d4-8ae0da7d3d9e",
"metadata": {},
"source": [
"But what if the point trying `%run nbb.ipynb` was to see the result of that notebook here without any fancy use of python code /`nbformat`?\n",
"\n",
"Jupytext can be used to make the notebook a script and then the code can be run with `!python`, or possibly `%run -p` but [that has not been my experience](https://stackoverflow.com/a/68023750/8508004). \n",
"Illustrating that:"
]
},
{
"cell_type": "markdown",
"id": "5d67a553-3ac6-4dec-879f-286b0d6e1a40",
"metadata": {},
"source": [
"Convert nbb.ipynb to a script using Jupytext."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "2681dd61-a851-4743-b99c-5dc8a0a1112d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[jupytext] Reading nbb.ipynb in format ipynb\n",
"[jupytext] Writing nbb.py\n"
]
}
],
"source": [
"!jupytext --to py nbb.ipynb"
]
},
{
"cell_type": "markdown",
"id": "5ff4c9a9-0fc8-4c42-8038-2a52a9ed1032",
"metadata": {},
"source": [
"Since it has IPython conveniences though we want it as `.ipy`. \n",
"Jupytext doesn't recognize IPython though when making Python code, and so it comments it out and we need to fix that, too"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9697cb67-0334-4bf2-9cde-42040303470d",
"metadata": {},
"outputs": [],
"source": [
"!mv nbb.py nbb.ipy\n",
"import os\n",
"ipython_file = \"nbb.ipy\"\n",
"with open(ipython_file, 'r') as thefile:\n",
" script=thefile.read()\n",
"fixed_script = script.replace(\"# %\",\"%\")\n",
"with open(ipython_file, 'w') as output_file:\n",
" output_file.write(fixed_script)"
]
},
{
"cell_type": "markdown",
"id": "fcd514f2-e930-4a02-844d-a16ba94b2e87",
"metadata": {},
"source": [
"Obviously this preparation could all be done in one cell, but I broke it up to make annotate the steps better. \n",
"Also keep in mind we would have been done with `!jupytext --to py nbb.ipynb` if the code in the notebook had been pure Python. \n",
"\n",
"With that preparation complete..."
]
},
{
"cell_type": "markdown",
"id": "a96ee49b-6c86-4e4c-a971-65cff9ac4336",
"metadata": {},
"source": [
"Now run the script in a separate namespace where won't pass back the settings."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e0454c3b-2be6-4453-9b83-f631bc0792e6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[?12l\u001b[?25h\u001b[22;0t\u001b]0;IPython: jovyan/notebooks\u0007y=100\n",
"y=150\n",
"\u001b[23;0t"
]
}
],
"source": [
"!ipython nbb.ipy"
]
},
{
"cell_type": "markdown",
"id": "38506078-d7d9-4116-8991-e104dda3848b",
"metadata": {},
"source": [
"That doesn't look great, but the results are visible."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "7aca7f9b-77d4-4f36-8b9c-4085253bdcf7",
"metadata": {},
"outputs": [],
"source": [
"# Seems like below should work in theory but DOES NOT BECAUSE IT ADJUSTS `y` in this namespace, too, see https://stackoverflow.com/a/68023750/8508004\n",
"#%run -p nbb.ipy"
]
},
{
"cell_type": "markdown",
"id": "6e6d84d6-4592-462f-afed-6a84217965bc",
"metadata": {},
"source": [
"Check `y` here in this notebook's namespace is still intact."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "7da0b88c-f677-4e79-b505-c5f56cab5d47",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"y=100\n"
]
}
],
"source": [
"print(f\"{y=}\")"
]
},
{
"cell_type": "markdown",
"id": "04f665d0-310e-4a80-b9fe-dd69758cbb5b",
"metadata": {},
"source": [
"`y` value here didn't get changed when nbb code run."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment