Skip to content

Instantly share code, notes, and snippets.

@fomightez
Created January 3, 2023 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fomightez/1877981842f7cae13021f49ce21a8f5b to your computer and use it in GitHub Desktop.
Save fomightez/1877981842f7cae13021f49ce21a8f5b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "416420f4-b62a-41f7-a1cf-c4eee116ed8a",
"metadata": {},
"source": [
"# From inside already-running Jupyter\n",
"\n",
"Imagine you have your developing code file you were editing with your text editor elswhere. We'll make `myobj.py` to simulate that:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "77b52136-eae7-4bea-a536-1a07221e6581",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing 's' (str) to file 'myobj.py'.\n"
]
}
],
"source": [
"s = '''class Myobj(object):\n",
" def __init__(self,arg1,arg2):\n",
" self.a1 = arg1\n",
" self.a2 = arg2\n",
" def doSomething(self):\n",
" from IPython import embed; embed() # breakpoint()\n",
"\n",
"test = Myobj(2,3)\n",
"test.doSomething()'''\n",
"%store s >myobj.py"
]
},
{
"cell_type": "markdown",
"id": "d8ac566b-1906-433e-a0b5-7c84721a9cb9",
"metadata": {},
"source": [
"Now in Jupyter, say you define your settings and want to test with your developing code. Let's define the settings to test by running the next cell:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "54a3aa51-ac11-45f9-8697-dde21b5f7097",
"metadata": {},
"outputs": [],
"source": [
"j = 1\n",
"t = 'test string'"
]
},
{
"cell_type": "markdown",
"id": "5c258b4b-fedc-4c9a-8318-78d317b47c0b",
"metadata": {},
"source": [
"Now to test the current script we can run it attached to this active kernel using the `-i` flag, see [%run documentation](https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-run):"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "479ec82f-418e-4a34-8fcb-43a120f29a1d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Python 3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:35:26) [GCC 10.4.0]\n",
"Type 'copyright', 'credits' or 'license' for more information\n",
"IPython 8.5.0 -- An enhanced Interactive Python. Type '?' for help.\n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"In [1]: j\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Out[1]: 1\n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"In [2]: t\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Out[2]: 'test string'\n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"In [3]: self.a1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Out[3]: 2\n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"In [4]: exit()\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"%run -i myobj.py"
]
},
{
"cell_type": "markdown",
"id": "9f27ada9-c0c5-4c37-a5d1-ab001a6b9530",
"metadata": {},
"source": [
"That should give you what you want. Imagine that is a notebook itself. Just those last two cells!\n",
"\n",
"Now Imagine over in your text editor you change the code. To simulate that run the next cell:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "406b1a05-bc47-4c83-bd2b-3ba8aa365b5c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing 's' (str) to file 'myobj.py'.\n"
]
}
],
"source": [
"s = '''class Myobj(object):\n",
" def __init__(self,arg1,arg2):\n",
" self.a1 = arg1\n",
" self.a2 = arg2\n",
" def doSomething(self):\n",
" print('THIS IS A CHANGED VERSION')\n",
" print(' ')\n",
" print(' ')\n",
" from IPython import embed; embed() # breakpoint()\n",
"\n",
"test = Myobj(2,3)\n",
"test.doSomething()'''\n",
"%store s >myobj.py"
]
},
{
"cell_type": "markdown",
"id": "9e4b925e-15eb-460b-b8b8-cd276bc262da",
"metadata": {},
"source": [
"Now if we really started with the imaginary notebook that just had the two cells, we could just hit the keyboard shortcut for Restart Kernel and Run All cells. But we'll imagine that by running the code again and querying the state:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "64ccd623-9edd-4b43-89b5-974c4e46dc6c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"THIS IS A CHANGED VERSION\n",
" \n",
" \n",
"Python 3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:35:26) [GCC 10.4.0]\n",
"Type 'copyright', 'credits' or 'license' for more information\n",
"IPython 8.5.0 -- An enhanced Interactive Python. Type '?' for help.\n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"In [1]: j\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Out[1]: 1\n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"In [2]: t\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Out[2]: 'test string'\n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"In [3]: self.ai\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"---------------------------------------------------------------------------\n",
"AttributeError Traceback (most recent call last)\n",
"Cell In [3], line 1\n",
"----> 1 self.ai\n",
"\n",
"AttributeError: 'Myobj' object has no attribute 'ai'\n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"In [4]: self.a1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Out[4]: 2\n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"In [5]: exit()\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"%run -i myobj.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c38f68a-f805-40af-aa3a-3b9b0d54a550",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "2db3ca17-cf06-47b7-8ec7-5fc38bee274b",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment