{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# QR Codes Are Magic\n", "While really ugly, QR codes are robust and widely implemented. Like JSON. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " pip install qrcode\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " conda install -c conda-forge qrcode" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ " import qrcode as qrcode, qrcode.image.svg, IPython; get_ipython = IPython.get_ipython\n", " Ø = __name__ == '__main__'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " \n", "and you can make QR codes out of arbitrary data which can be read by most cell phones.\n", "\n", "Here's a magic which gives you all kinds of toys to play with." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ " @IPython.core.magic.register_line_cell_magic\n", " def qr(line, cell=None):\n", " line = (line + \" \" + (cell or \"\")).strip()\n", " args = parser.parse_args(line.split())\n", " q = qrcode.QRCode(version=args.version, error_correction=args.error_correction,\n", " box_size=args.size, border=args.border, mask_pattern=args.mask,)\n", " q.add_data(args.data), q.make(fit=True)\n", " \n", " img = None\n", " if args.output == \"png\":\n", " img = q.make_image(fill_color=args.fg, back_color=args.bg)\n", " elif \"svg\" in args.output:\n", " with __import__('io').BytesIO() as out:\n", " q.make_image(fill_color=args.fg, back_color=args.bg, image_factory={\n", " \"svg\": qrcode.image.svg.SvgImage,\n", " \"svg-fragment\": qrcode.image.svg.SvgFragmentImage,\n", " \"svg-path\": qrcode.image.svg.SvgPathImage,\n", " }[args.output])._write(out)\n", " img = IPython.display.SVG(out.getvalue())\n", " elif args.output == \"ascii\":\n", " with IPython.utils.capture.capture_output() as cap: q.print_ascii()\n", "\n", " img = IPython.display.HTML(f\"\"\"
\\n{cap.stdout}\\n\"\"\")\n",
" IPython.display.display(img)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`qrcode` hides it's parser inside a closure, so let's make another one."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
" parser = __import__('argparse').ArgumentParser()\n",
" parser.add_argument('data')\n",
" parser.add_argument('-s', '--size', default=10, type=int)\n",
" parser.add_argument('-b', '--border', default=1, type=int)\n",
" parser.add_argument('-e', '--error-correction', default=1, type=int)\n",
" parser.add_argument('-v', '--version', default=1)\n",
" parser.add_argument('--fg', default='black')\n",
" parser.add_argument('--bg', default='white')\n",
" parser.add_argument('-m', '--mask', default=None, type=int)\n",
" parser.add_argument('-o', '--output', default='png', choices=['svg', 'svg-path', 'svg-fragment', 'png', 'ascii']);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Outputs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### SVG output"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
" def _jupyter_qr():\n",
" %qr -o svg-path https://jupyter.org\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plain Text\n",
"\n",
"A plain text QR to post on your In addition to images, you can generate plain text, which would look great on your BBS!"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
" def _pydata_qr():\n",
" %qr -o ascii https://pydata.org"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Edit your own QR code with an IPython widget."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
" from ipywidgets import interact, ColorPicker\n",
" def deathbeds(url=\"https://deathbeds.github.io\", size=(1,10), mask=(0,7), error=(0,3), version=(1,8), border=(0, 10), fore=ColorPicker(value=\"#0f0\"), back=ColorPicker(value=\"black\")):\n",
" %qr -s{size} -m{mask} -e{error} -v{version} -b{border} --fg {fore} --bg {back} {url}\n",
" \n",
" def _interactive_editor(): interact(deathbeds)"
]
}
],
"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
}