Skip to content

Instantly share code, notes, and snippets.

@koi8-r
Last active December 8, 2019 16:15
Show Gist options
  • Save koi8-r/aa127d07f217cb3fd15c0f2d366779dc to your computer and use it in GitHub Desktop.
Save koi8-r/aa127d07f217cb3fd15c0f2d366779dc to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 141,
"metadata": {},
"outputs": [],
"source": [
"from IPython.core.magic import register_cell_magic\n",
"from IPython import get_ipython\n",
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"\n",
"@register_cell_magic\n",
"def query(line: str, cell: str):\n",
" call = cell.format(**globals())\n",
" ns_name = line.strip()\n",
" # pd.read_sql(cell, conn)\n",
" res = {\n",
" 'i': filter(bool, cell.replace(\"\\n\", '').split(\"|\"))\n",
" }\n",
" if ns_name.strip('-'):\n",
" get_ipython().user_ns[line.strip()] = res\n",
"\n",
" return pd.DataFrame(res)\n",
"\n",
"del query"
]
},
{
"cell_type": "code",
"execution_count": 142,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>i</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>abc</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>x(123)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>z</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" i\n",
"0 abc\n",
"1 x(123)\n",
"2 z"
]
},
"execution_count": 142,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%query\n",
"abc\n",
"| x(123)\n",
"| z"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$X(A \\mid B) = \\frac{A}{B}$"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [],
"source": [
"import aiohttp\n",
"\n",
"\n",
"async with aiohttp.ClientSession() as ua:\n",
" async with ua.get('http://httpbin.org/get') as resp:\n",
" res = await resp.json()"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {},
"outputs": [
{
"data": {
"application/json": {
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "Python/3.7 aiohttp/3.6.2"
},
"origin": "185.97.201.94, 185.97.201.94",
"url": "https://httpbin.org/get"
},
"text/plain": [
"<IPython.core.display.JSON object>"
]
},
"execution_count": 125,
"metadata": {
"application/json": {
"expanded": true,
"root": "root"
}
},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import JSON, GeoJSON, HTML\n",
" \n",
"JSON(res, expanded=True)"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<a href=\"/\">123</a>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import HTML\n",
"\n",
"\n",
"HTML('<a href=\"/\">123</a>')"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>XYZ</div>"
],
"text/plain": [
"<__main__.HtmlVW object>"
]
},
"execution_count": 133,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import DisplayObject\n",
"\n",
"\n",
"class HtmlVW(DisplayObject):\n",
" \"\"\"\n",
" See also:\n",
" - https://github.com/mpacer/display_xml/blob/master/display_xml/xml.py\n",
" - https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html\n",
" \"\"\"\n",
" _data = None\n",
" def __init__(self, data=None, url=None, filename=None):\n",
" super().__init__(data=data, url=url, filename=filename)\n",
"\n",
" @property\n",
" def data(self):\n",
" return self._data\n",
"\n",
" @data.setter\n",
" def data(self, data):\n",
" self._data = data\n",
"\n",
" def _repr_html_(self):\n",
" return \"\"\"<div>XYZ</div>\"\"\"\n",
"\n",
"HtmlVW({'x': 'XYZ'})"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[0;31mSignature:\u001b[0m \u001b[0mnbformat\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mv4\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnew_output\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutput_type\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mDocstring:\u001b[0m Create a new output, to go in the ``cell.outputs`` list of a code cell.\n",
"\u001b[0;31mFile:\u001b[0m ~/app/jupyter/lib/python3.7/site-packages/nbformat/v4/nbbase.py\n",
"\u001b[0;31mType:\u001b[0m function\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import nbformat\n",
"\n",
"\n",
"nbformat.v4.new_output?"
]
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Hello World"
],
"text/xml": [
"<h1>Hello World</h1>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import display\n",
"\n",
"# https://jupyterlab.readthedocs.io/en/stable/user/file_formats.html\n",
"display({'text/xml': '<h1>Hello World</h1>', 'text/plain': 'Hello World'}, raw=True)"
]
}
],
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment