Skip to content

Instantly share code, notes, and snippets.

@fomightez
Created June 16, 2024 01:10
Show Gist options
  • Save fomightez/910bcf7cb04a0caab9ac98244655fd32 to your computer and use it in GitHub Desktop.
Save fomightez/910bcf7cb04a0caab9ac98244655fd32 to your computer and use it in GitHub Desktop.
For How to get output out of `.ipynb` files as text 6-15-204 SO 78627956/8508004 https://stackoverflow.com/q/78627956/8508004
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "e2cc1730-f680-4729-bd22-4ca50c185794",
"metadata": {},
"source": [
"# FOR SO 78627956/8508004\n",
"\n",
"for https://stackoverflow.com/q/78627956/8508004\n",
"\n",
"----\n",
"\n",
"## Try first with Jupytext\n",
"\n",
"Since it is more convenitent than `nbformat`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a863cae0-6ab1-4a2f-a745-dd6b6cdd0d1a",
"metadata": {},
"outputs": [],
"source": [
"%pip install jupytext"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "fcdbeb34-e834-4776-8c16-61f726b908d8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"index.ipynb README.md requirements.txt supplied.ipynb\n",
"LICENSE requirements.in runtime.txt Untitled.ipynb\n"
]
}
],
"source": [
"ls"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d17f2b0f-f944-41bb-ad5a-f3a74ed3b40c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"cells\": [\n",
" {\n",
" \"cell_type\": \"code\",\n",
" \"execution_count\": 14,\n",
" \"id\": \"48f1dc25-9bc7-4a33-a9dd-b47480856414\",\n",
" \"metadata\": {},\n",
" \"outputs\": [\n",
" {\n",
" \"data\": {\n",
" \"text/html\": [\n",
" \"<div><img alt=\\\"Output\\\" src=\\\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADwAAAA3CAIAAADfWb7mAAAA0npUWHRSYXcgcHJvZmlsZSB0eXBlIGV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]\n",
" },\n",
" \"execution_count\": 14,\n",
" \"metadata\": {\n",
" \"text/html\": [],\n",
" \"text/plain\": []\n",
" },\n",
" \"output_type\": \"execute_result\"\n",
" }\n",
" ],\n",
" \"source\": [\n",
" \"f[x]=(a*x+b)/(c*x+d)\"\n",
" ]\n"
]
}
],
"source": [
"!head -n 30 supplied.ipynb"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "92989a3a-bc43-4e7f-877d-bef1009f31fc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" \"mimetype\": \"text/x-python\",\n",
" \"name\": \"python\",\n",
" \"nbconvert_exporter\": \"python\",\n",
" \"pygments_lexer\": \"ipython3\",\n",
" \"version\": \"3.10.10\"\n",
" }\n",
" },\n",
" \"nbformat\": 4,\n",
" \"nbformat_minor\": 5\n",
"}\n"
]
}
],
"source": [
"!tail supplied.ipynb"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b544141d-ccf7-4169-a69e-02fbd37be6b8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[jupytext] Reading supplied.ipynb in format ipynb\n",
"[jupytext] Writing supplied.md\n"
]
}
],
"source": [
"!jupytext --to md supplied.ipynb # based on https://jupytext.readthedocs.io/en/latest/using-cli.html"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ff486650-c7ee-43c4-b83b-2940d81dfae8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"---\n",
"jupyter:\n",
" jupytext:\n",
" text_representation:\n",
" extension: .md\n",
" format_name: markdown\n",
" format_version: '1.3'\n",
" jupytext_version: 1.16.2\n",
" kernelspec:\n",
" display_name: Python 3 (ipykernel)\n",
" language: python\n",
" name: python3\n",
"---\n",
"\n",
"```python\n",
"f[x]=(a*x+b)/(c*x+d)\n",
"```\n",
"\n",
"```python\n",
"sol=Solve[f[x]==x,x,Reals]\n",
"```\n",
"\n",
"```python\n",
"\n",
"```\n"
]
}
],
"source": [
"cat supplied.md"
]
},
{
"cell_type": "markdown",
"id": "9bb077e6-582e-4ed7-b3e2-d5c8a9f33434",
"metadata": {},
"source": [
"Jupytext fails to include the output. Dang, but seems consistent with documentation:\n",
"\n",
">\"The text representation only contains the part of the notebook that you wrote (not the outputs). You get a cleaner diff history\" - source: [here](https://jupytext.readthedocs.io/en/latest/faq.html#can-i-create-a-notebook-from-a-text-file)\n",
"\n",
"(There is a way around this with pairing but that won't help in this case since I don't want to run the notebook since already run and want those outouts.)\n",
"\n",
"------\n",
"\n",
"## Try with nbformat\n",
"\n",
"I have [code that deals with notebook ouptut via nbformat here](https://stackoverflow.com/a/73298254/8508004) that deals with output:\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "56f7a14e-84a9-41b9-ae3b-db39e818a9c2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[15, 14, None]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#from https://stackoverflow.com/a/73298254/8508004\n",
"import os\n",
"notebook_example = \"supplied.ipynb\"\n",
"### Use nbformat to get estimate of output size from code cells. #########\n",
"import nbformat as nbf\n",
"ntbk = nbf.read(notebook_example, nbf.NO_CONVERT)\n",
"size_estimate_dict = {}\n",
"for cell in ntbk.cells:\n",
" if cell.cell_type == 'code':\n",
" size_estimate_dict[cell.execution_count] = len(str(cell.outputs))\n",
"out_size_info = [k for k, v in sorted(size_estimate_dict.items(), key=lambda item: item[1],reverse=True)]\n",
"out_size_info"
]
},
{
"cell_type": "markdown",
"id": "4ae7eefd-d219-4b07-a6f3-d6c4991dad3c",
"metadata": {},
"source": [
"Modifying that to not sort and to show as text the output is below.\n",
"\n",
"\n",
"OP called the file `debug.ipynb`. So use that or real name in place of `supplied.ipynb in code below, similar for `suplied_nb_output_text.txt` where you'll want to edit it to what you want:"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "45d1481a-6e2b-4546-beb8-7fe0049c8ace",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b + a x\n",
"-------\n",
"d + c x\n",
" 2 2\n",
" a + 4 b c - 2 a d + d\n",
" Sqrt[-----------------------]\n",
" 2\n",
" a - d c\n",
"{{x -> ConditionalExpression[----- - -----------------------------, \n",
" 2 c 2\n",
" \n",
" 2 2 2 2\n",
" -a + 2 a d - d -a + 2 a d - d\n",
"> (b > ---------------- && c > 0) || (b < ---------------- && c < 0)]}, \n",
" 4 c 4 c\n",
" \n",
" 2 2\n",
" a + 4 b c - 2 a d + d\n",
" Sqrt[-----------------------]\n",
" 2\n",
" a - d c\n",
"> {x -> ConditionalExpression[----- + -----------------------------, \n",
" 2 c 2\n",
" \n",
" 2 2 2 2\n",
" -a + 2 a d - d -a + 2 a d - d\n",
"> (b > ---------------- && c > 0) || (b < ---------------- && c < 0)]}}\n",
" 4 c 4 c\n"
]
}
],
"source": [
"import os\n",
"notebook_example = \"supplied.ipynb\"\n",
"import nbformat as nbf\n",
"ntbk = nbf.read(notebook_example, nbf.NO_CONVERT)\n",
"text_collected = \"\"\n",
"for cell in ntbk.cells:\n",
" if cell.cell_type == 'code':\n",
" # parse out the `text/plain` part of any real output\n",
" if cell.outputs:\n",
" text_collected += cell.outputs[0]['data']['text/plain']+\"\\n\"\n",
" print(cell.outputs[0]['data']['text/plain'])\n",
"# Write collected text to a file\n",
"with open('suplied_nb_output_text.txt', 'w') as output_handler:\n",
" output_handler.write(text_collected)"
]
},
{
"cell_type": "markdown",
"id": "7f239a4d-fbb7-4674-9478-6e42be1363fe",
"metadata": {},
"source": [
"That code also saves the text content of the output as a text file."
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "af529723-8f0d-4593-b1a6-4f7b6fa2e1fd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b + a x\n",
"-------\n",
"d + c x\n",
" 2 2\n",
" a + 4 b c - 2 a d + d\n",
" Sqrt[-----------------------]\n",
" 2\n",
" a - d c\n",
"{{x -> ConditionalExpression[----- - -----------------------------, \n",
" 2 c 2\n",
" \n",
" 2 2 2 2\n",
" -a + 2 a d - d -a + 2 a d - d\n",
"> (b > ---------------- && c > 0) || (b < ---------------- && c < 0)]}, \n",
" 4 c 4 c\n",
" \n",
" 2 2\n",
" a + 4 b c - 2 a d + d\n",
" Sqrt[-----------------------]\n",
" 2\n",
" a - d c\n",
"> {x -> ConditionalExpression[----- + -----------------------------, \n",
" 2 c 2\n",
" \n",
" 2 2 2 2\n",
" -a + 2 a d - d -a + 2 a d - d\n",
"> (b > ---------------- && c > 0) || (b < ---------------- && c < 0)]}}\n",
" 4 c 4 c\n"
]
}
],
"source": [
"cat suplied_nb_output_text.txt"
]
}
],
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment