{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Ipyrest, XKCD Examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipyrest import Api\n",
    "\n",
    "url = 'https://xkcd.com/552/info.0.json'\n",
    "Api(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from ipyrest import Api\n",
    "\n",
    "url = requests.get('https://xkcd.com/552/info.0.json').json()['img']\n",
    "Api(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from ipywidgets import Image\n",
    "from ipyrest import Api\n",
    "from ipyrest.responseviews import ResponseView, builtin_view_classes\n",
    "\n",
    "class XKCDView(ResponseView):\n",
    "    \"Api rendering view for XKCD comics taken from XKCD JSON API.\"\n",
    "    name = 'XKCD'\n",
    "    mimetype_pats = ['application/json']\n",
    "    def render(self, resp):\n",
    "        return Image(value=requests.get(resp.json()['img']).content)\n",
    "\n",
    "url = 'https://xkcd.com/552/info.0.json'\n",
    "Api(url, views=builtin_view_classes + [XKCDView])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Latest Comic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'https://xkcd.com/info.0.json'\n",
    "Api(url, views=builtin_view_classes + [XKCDView])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compare with [xkcd.com](https://xkcd.com)..."
   ]
  }
 ],
 "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.3"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}