Skip to content

Instantly share code, notes, and snippets.

@sirex
Last active October 17, 2017 20:21
Show Gist options
  • Save sirex/f8064d588fc518de2382f309390e1cf7 to your computer and use it in GitHub Desktop.
Save sirex/f8064d588fc518de2382f309390e1cf7 to your computer and use it in GitHub Desktop.
VilniusPy (2017-10-18)
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dark Jupyter theme\n",
"\n",
"https://github.com/powerpak/jupyter-dark-theme\n",
"\n",
"\n",
"```\n",
"mkdir -p ~/.jupyter/custom\n",
"wget https://github.com/powerpak/jupyter-dark-theme/raw/master/custom.css \\\n",
" -O ~/.jupyter/custom/custom.css\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Jupyter vim-mode\n",
"\n",
"https://github.com/lambdalisue/jupyter-vim-binding"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Jupyter as a calculator"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"2 + 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"_**4"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"math.log2(_2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Jupyter for math"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sympy\n",
"sympy.init_printing(use_latex='mathjax')\n",
"x = sympy.symbols('x')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x**2 / 3 + 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sympy.solve(_, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(sympy.latex(_5))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$\\LaTeX$ works in markdown cells too: $\\frac{x^{2}}{3} + 1$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Finding things in Jupyter"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"str.is*?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"str.isalnum?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Execute shell commands from Jupyter"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!screenfetch"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lines = !ls -l"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lines"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"/print lines.n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lines.s"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lines.grep('ipynb')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lines.grep('ipynb').fields(-1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lines.grep('ipynb').fields(-1).p"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for p in lines.grep('ipynb').fields(-1).p:\n",
" !echo $p.suffix {p.suffix.lstrip('.')}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for s, p in map(str.split, lines.grep('ipynb').fields(4, -1)):\n",
" du = !du -sh $p\n",
" !echo {du.fields(0).s} $s {du.fields(1).s}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!echo $USER"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Jupyter variable storage"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%store lines"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%store"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Run doctests from Jupyter"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from collections import deque\n",
"\n",
"def fib(n):\n",
" \"\"\"\n",
" >>> fib(10)\n",
" [0, 1, 2, 3, 5, 8, 13, 21, 34, 55]\n",
" \"\"\"\n",
" q = deque([0, 1], 2)\n",
" return [q.append(sum(q)) or q[0] for i in range(2, n + 2)]\n",
"\n",
"\n",
"import doctest\n",
"\n",
"doctest.run_docstring_examples(fib, None)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Profiling in Jupyter"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%time fib(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%timeit fib(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%prun fib(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%load_ext line_profiler\n",
"\n",
"%lprun -f fib fib(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Requires `line_profiler` package."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Working with databases from Jupyter\n",
"\n",
"Requires `ipython-sql` package."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%load_ext sql"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%sql sqlite://\n",
" \n",
"CREATE TABLE data (a, b);\n",
"\n",
"INSERT INTO data VALUES\n",
" ('a', 1),\n",
" ('b', 2),\n",
" ('c', 3),\n",
" ('d', 4),\n",
" ('e', 5),\n",
" ('f', 6);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%sql select * from data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"v = 3\n",
"%sql select * from data where b = :v"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = %sql select * from data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data.DataFrame().query('b > 3')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Showing plots in Jupyter notebook"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"\n",
"import matplotlib as mpl\n",
"\n",
"mpl.style.use('seaborn-darkgrid')\n",
"mpl.rc('figure', figsize=(12, 8))\n",
"mpl.rc('font', size=18)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data.bar();"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data.pie();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Interactive Jupyter\n",
"\n",
"Requires `ipywidgets` packages:\n",
"\n",
" pip install ipywidgets\n",
" jupyter nbextension enable --py widgetsnbextension"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import interactive\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"def f(m, b):\n",
" plt.figure(2)\n",
" x = np.linspace(-10, 10, num=100)\n",
" plt.axhline(0, color='gray')\n",
" plt.axvline(0, color='gray')\n",
" plt.plot(x, m * x + b, linewidth=3)\n",
" plt.ylim(-5, 5)\n",
" plt.show()\n",
"\n",
"interactive(f, m=(-2.0, 2.0), b=(-3, 3, 0.5))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Embeding videos into Jupyter notebook"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib as mpl\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.animation as animation\n",
"\n",
"mpl.rc('animation', html='html5')\n",
"\n",
"fig, ax = plt.subplots(figsize=(5, 5))\n",
"x = np.linspace(-10, 10, num=100)\n",
"b = np.sin(np.linspace(-np.pi, np.pi, 200))\n",
"ax.axhline(0, color='gray')\n",
"ax.axvline(0, color='gray')\n",
"ax.set_xlim(-5, 5)\n",
"ax.set_ylim(-5, 5)\n",
"line, = ax.plot([], [], lw=3)\n",
"plt.close()\n",
"\n",
"def update(b):\n",
" line.set_data(x, b * x)\n",
" return line,\n",
"\n",
"animation.FuncAnimation(fig, update, frames=b, interval=24)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sharing Jupyter notebooks\n",
"\n",
"https://gist.github.com/sirex/f8064d588fc518de2382f309390e1cf7\n",
"\n",
"http://nbviewer.jupyter.org/gist/sirex/f8064d588fc518de2382f309390e1cf7"
]
}
],
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment