diff --git a/degbugging_notebooks/special_video_cases.ipynb b/degbugging_notebooks/special_video_cases.ipynb index 587d112..ae8d00f 100644 --- a/degbugging_notebooks/special_video_cases.ipynb +++ b/degbugging_notebooks/special_video_cases.ipynb @@ -2,102 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, - "id": "63fa3f08-7226-4006-b2e5-46b502c09452", - "metadata": {}, - "outputs": [], - "source": [ - "from IPython import get_ipython\n", - "from IPython.core import magic_arguments\n", - "from IPython.core.magic import Magics, cell_magic, magics_class\n", - "from IPython.display import display\n", - "from IPython.utils.capture import capture_output\n", - "from pprint import pprint\n", - "from pathlib import Path\n", - "import re\n", - "from base64 import b64decode" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "bc8c9994-11cb-485f-9b63-ea0322c665f2", - "metadata": {}, - "outputs": [], - "source": [ - "@magics_class\n", - "class CaptureMagic(Magics):\n", - " @magic_arguments.magic_arguments()\n", - " @magic_arguments.argument(\n", - " \"--path\",\n", - " \"-p\",\n", - " default=None,\n", - " help=(\n", - " \"The path where the video will be saved to. When there is more then one video, multiple paths have to be defined\"\n", - " ),\n", - " )\n", - " @cell_magic\n", - " def capture_video(self, line, cell):\n", - " args = magic_arguments.parse_argstring(CaptureMagic.capture_video, line)\n", - " paths_pathlib = args.path.strip('\"').split(\" \")\n", - " with capture_output(stdout=False, stderr=False, display=True) as result:\n", - " self.shell.run_cell(cell)\n", - " for output in result.outputs:\n", - " display(output) # only disabled for debugging\n", - " global data # for debugging\n", - " data = output.data\n", - "\n", - " pprint(data) # for debugging\n", - "\n", - " print(\"#####\")\n", - "\n", - " if \"text/html\" in data:\n", - " path = paths_pathlib.pop(0)\n", - " if not path:\n", - " raise ValueError(\"Too few paths given!\")\n", - " video_object_html_string = data[\"text/html\"]\n", - "\n", - " # Extract video source filename\n", - " pattern = re.compile(r'video src=\"(.+?)\"')\n", - " match = pattern.search(video_object_html_string)\n", - "\n", - " if match:\n", - " video_dir = match.group(1)\n", - " print(f\"Found video at {video_dir}\")\n", - "\n", - " dest = Path(path)\n", - " src = Path(video_dir)\n", - " dest.write_bytes(src.read_bytes())\n", - "\n", - " else: # debugging only\n", - " print(\"No video source found in HTML string.\")\n", - "\n", - " # Extract base64-encoded embedded video data \n", - " pattern = re.compile(r'data:video/mp4;base64,(.*)\">')\n", - " match = pattern.search(video_object_html_string)\n", - "\n", - " if match:\n", - " base64_string = match.group(1)\n", - " print(\"Found base64-encoded video\")\n", - "\n", - " # Decode base64 string and save video file using pathlib\n", - " video_bytes = b64decode(base64_string)\n", - " assert isinstance(video_bytes, bytes)\n", - " dest = Path(path)\n", - " dest.write_bytes(video_bytes)\n", - "\n", - " else:\n", - " print(\"No base64-encoded video data found in HTML string.\")\n", - "\n", - "\n", - "\n", - "ipy = get_ipython()\n", - "ipy.register_magics(CaptureMagic)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "5c4d82bd", "metadata": {}, "outputs": [], @@ -107,184 +12,30 @@ }, { "cell_type": "code", - "execution_count": 4, - "id": "39964c22", - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'text/html': '',\n", - " 'text/plain': ''}\n", - "#####\n", - "Found video at ../assets/dog_with_water.mp4\n", - "No base64-encoded video data found in HTML string.\n" - ] - } - ], - "source": [ - "%%capture_video -p \"not_embedded.mp4\"\n", - "# ✅✅✅\n", - "Video(\"../assets/dog_with_water.mp4\", embed=False, width=300)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, + "execution_count": null, "id": "6924675f", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'text/html': '',\n", - " 'text/plain': ''}\n", - "#####\n", - "No video source found in HTML string.\n", - "Found base64-encoded video\n" - ] - } - ], + "outputs": [], "source": [ - "%%capture_video -p \"embedded.mp4\"\n", "# ✅✅✅\n", "Video(\"../assets/dog_with_water.mp4\",embed=True,width=300)" ] }, { - "cell_type": "code", - "execution_count": 6, - "id": "960d42bc", - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'text/html': '',\n", - " 'text/plain': ''}\n", - "#####\n", - "Found video at https://github.com/Octoframes/jupyter_capture_output/blob/f8968519e9ae161c56f126f8d2cf155795fdde86/assets/dog_with_water.mp4?raw=true\n" - ] - }, - { - "ename": "FileNotFoundError", - "evalue": "[Errno 2] No such file or directory: 'https:/github.com/Octoframes/jupyter_capture_output/blob/f8968519e9ae161c56f126f8d2cf155795fdde86/assets/dog_with_water.mp4?raw=true'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[6], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m get_ipython()\u001b[39m.\u001b[39;49mrun_cell_magic(\u001b[39m'\u001b[39;49m\u001b[39mcapture_video\u001b[39;49m\u001b[39m'\u001b[39;49m, \u001b[39m'\u001b[39;49m\u001b[39m-p \u001b[39;49m\u001b[39m\"\u001b[39;49m\u001b[39mweb.mp4\u001b[39;49m\u001b[39m\"\u001b[39;49m\u001b[39m'\u001b[39;49m, \u001b[39m'\u001b[39;49m\u001b[39m# ❌❌❌\u001b[39;49m\u001b[39m\\n\u001b[39;49;00m\u001b[39mVideo(\u001b[39;49m\u001b[39m\"\u001b[39;49m\u001b[39mhttps://github.com/Octoframes/jupyter_capture_output/blob/f8968519e9ae161c56f126f8d2cf155795fdde86/assets/dog_with_water.mp4?raw=true\u001b[39;49m\u001b[39m\"\u001b[39;49m\u001b[39m,width=300)\u001b[39;49m\u001b[39m\\n\u001b[39;49;00m\u001b[39m'\u001b[39;49m)\n", - "File \u001b[0;32m~/projects/jupyter-capture-output/.venv/lib/python3.11/site-packages/IPython/core/interactiveshell.py:2430\u001b[0m, in \u001b[0;36mInteractiveShell.run_cell_magic\u001b[0;34m(self, magic_name, line, cell)\u001b[0m\n\u001b[1;32m 2428\u001b[0m \u001b[39mwith\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mbuiltin_trap:\n\u001b[1;32m 2429\u001b[0m args \u001b[39m=\u001b[39m (magic_arg_s, cell)\n\u001b[0;32m-> 2430\u001b[0m result \u001b[39m=\u001b[39m fn(\u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n\u001b[1;32m 2432\u001b[0m \u001b[39m# The code below prevents the output from being displayed\u001b[39;00m\n\u001b[1;32m 2433\u001b[0m \u001b[39m# when using magics with decodator @output_can_be_silenced\u001b[39;00m\n\u001b[1;32m 2434\u001b[0m \u001b[39m# when the last Python token in the expression is a ';'.\u001b[39;00m\n\u001b[1;32m 2435\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mgetattr\u001b[39m(fn, magic\u001b[39m.\u001b[39mMAGIC_OUTPUT_CAN_BE_SILENCED, \u001b[39mFalse\u001b[39;00m):\n", - "Cell \u001b[0;32mIn[2], line 43\u001b[0m, in \u001b[0;36mCaptureMagic.capture_video\u001b[0;34m(self, line, cell)\u001b[0m\n\u001b[1;32m 41\u001b[0m dest \u001b[39m=\u001b[39m Path(path)\n\u001b[1;32m 42\u001b[0m src \u001b[39m=\u001b[39m Path(video_dir)\n\u001b[0;32m---> 43\u001b[0m dest\u001b[39m.\u001b[39mwrite_bytes(src\u001b[39m.\u001b[39;49mread_bytes())\n\u001b[1;32m 45\u001b[0m \u001b[39melse\u001b[39;00m: \u001b[39m# debugging only\u001b[39;00m\n\u001b[1;32m 46\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mNo video source found in HTML string.\u001b[39m\u001b[39m\"\u001b[39m)\n", - "File \u001b[0;32m/opt/homebrew/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py:1050\u001b[0m, in \u001b[0;36mPath.read_bytes\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1046\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mread_bytes\u001b[39m(\u001b[39mself\u001b[39m):\n\u001b[1;32m 1047\u001b[0m \u001b[39m \u001b[39m\u001b[39m\"\"\"\u001b[39;00m\n\u001b[1;32m 1048\u001b[0m \u001b[39m Open the file in bytes mode, read it, and close the file.\u001b[39;00m\n\u001b[1;32m 1049\u001b[0m \u001b[39m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1050\u001b[0m \u001b[39mwith\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mopen(mode\u001b[39m=\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39mrb\u001b[39;49m\u001b[39m'\u001b[39;49m) \u001b[39mas\u001b[39;00m f:\n\u001b[1;32m 1051\u001b[0m \u001b[39mreturn\u001b[39;00m f\u001b[39m.\u001b[39mread()\n", - "File \u001b[0;32m/opt/homebrew/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py:1044\u001b[0m, in \u001b[0;36mPath.open\u001b[0;34m(self, mode, buffering, encoding, errors, newline)\u001b[0m\n\u001b[1;32m 1042\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39m\"\u001b[39m\u001b[39mb\u001b[39m\u001b[39m\"\u001b[39m \u001b[39mnot\u001b[39;00m \u001b[39min\u001b[39;00m mode:\n\u001b[1;32m 1043\u001b[0m encoding \u001b[39m=\u001b[39m io\u001b[39m.\u001b[39mtext_encoding(encoding)\n\u001b[0;32m-> 1044\u001b[0m \u001b[39mreturn\u001b[39;00m io\u001b[39m.\u001b[39;49mopen(\u001b[39mself\u001b[39;49m, mode, buffering, encoding, errors, newline)\n", - "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'https:/github.com/Octoframes/jupyter_capture_output/blob/f8968519e9ae161c56f126f8d2cf155795fdde86/assets/dog_with_water.mp4?raw=true'" - ] - } - ], - "source": [ - "%%capture_video -p \"web.mp4\"\n", - "# ❌❌❌\n", - "Video(\"https://github.com/Octoframes/jupyter_capture_output/blob/f8968519e9ae161c56f126f8d2cf155795fdde86/assets/dog_with_water.mp4?raw=true\",width=300)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0f2eaa2d", + "cell_type": "markdown", + "id": "26a575d1", "metadata": {}, - "outputs": [], "source": [ - "### maybe useful at one point in future\n", - "# from IPython.display import Video\n", - "# import requests\n", - "\n", - "# url = \"https://github.com/Octoframes/jupyter_capture_output/blob/f8968519e9ae161c56f126f8d2cf155795fdde86/assets/dog_with_water.mp4?raw=true\"\n", - "# filename = \"dog_with_water.mp4\"\n", - "\n", - "# response = requests.get(url)\n", - "\n", - "# with open(filename, \"wb\") as f:\n", - "# f.write(response.content)\n", - "# #" + "![matplotlib_dog.png](attachment:matplotlib_dog.png)\n", + "\n" ] }, { - "cell_type": "code", - "execution_count": 7, - "id": "26a575d1", + "attachments": {}, + "cell_type": "markdown", + "id": "bf823e60", "metadata": {}, - "outputs": [], - "source": [ - "from pathlib import Path\n", - "Path(\"not_embedded.mp4\").unlink()\n", - "Path(\"embedded.mp4\").unlink()\n", - "\n", - "try:\n", - " Path(\"web.mp4\").unlink()\n", - "except:\n", - " pass" - ] + "source": [] } ], "metadata": { @@ -303,7 +54,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.3" + "version": "3.11.4" }, "vscode": { "interpreter": {