{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Saving images in issues\n", "\n", "We often create issues to collectively share images relating to a project.\n", "\n", "Reincarnation has a nice example. https://github.com/deathbeds/reincarnation/issues/1" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [], "source": [ "import pandas\n", "from CommonMark.render.renderer import Renderer\n", "from CommonMark import Parser\n", "from ipywidgets import *\n", "\n", "from dataclasses import dataclass\n", "\n", "from IPython.utils.capture import capture_output" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [], "source": [ "class ImageFinder(Renderer):\n", " def image(self, node, entering): self.out(node.destination + '\\n')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`scrape_images_from_issue` will take {repo} and issue {id}. It will return the images in the markdown." ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [], "source": [ "@dataclass(order=True)\n", "class IssueScraper:\n", " repo: str = None\n", " id: int = None\n", " df: pandas.DataFrame = None\n", " \n", " def __post_init__(self):\n", " if self.df is None: \n", " self.df = pandas.read_json(\n", " f\"\"\"https://api.github.com/repos/{self.repo}/issues/{self.id}/comments\"\"\")\n", " \n", " def images(self): return self.df.body.apply(\n", " lambda x: ImageFinder().render(Parser().parse(x))\n", " ).apply(str.splitlines).apply(pandas.Series).stack().drop_duplicates()\n", " \n", " def _repr_markdown_(self): return ''.join(map('![]({0})\\n'.format, self.images()))\n", " \n", " def _repr_mimebundle_(self, **_):\n", " selector, image = Select(options=list(self.images())), HTML()\n", " dlink((selector, 'value'), (image, 'value'), \"\".format)\n", " with capture_output() as out: HBox(children=[selector, image])._ipython_display_()\n", " return {\n", " **out.outputs[0].data,\n", " 'text/markdown': self._repr_markdown_()}, {}" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [], "source": [ "def _sample_issues_from_reincarnation():\n", " return IssueScraper('deathbeds/reincarnation', 1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def _get_reincarnation_images():\n", " return scrape_images_from_issue('deathbeds/reincarnation', 1)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }