Skip to content

Instantly share code, notes, and snippets.

Created May 30, 2017 00:47
Show Gist options
  • Save anonymous/f157812ed37aff92c3dde93842597a14 to your computer and use it in GitHub Desktop.
Save anonymous/f157812ed37aff92c3dde93842597a14 to your computer and use it in GitHub Desktop.
Jupyter Magics Tutorial
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"heading_collapsed": true
},
"cell_type": "markdown",
"source": "# What are magics?\n\nMagics are a way to talk to the Jupyter notebook itself and lets to interface with elements outside the kernel."
},
{
"metadata": {
"heading_collapsed": true
},
"cell_type": "markdown",
"source": "# Some Must-Know Examples\n\nlisting magics, bash, timing, latex and HTML"
},
{
"metadata": {
"heading_collapsed": true,
"hidden": true
},
"cell_type": "markdown",
"source": "## `lsmagic`\n\nTells you what magics you have available"
},
{
"metadata": {
"hidden": true,
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "% lsmagic",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"heading_collapsed": true,
"hidden": true
},
"cell_type": "markdown",
"source": "## `!`\n\nLets you run shell commands, e.g. get the version of Pandas"
},
{
"metadata": {
"hidden": true,
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "! pip freeze | grep pandas",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"heading_collapsed": true,
"hidden": true
},
"cell_type": "markdown",
"source": "## `%time`\n\nWill time whatever you evaluate"
},
{
"metadata": {
"hidden": true,
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "%time\n\nfor i in range(0, 1000000000):\n continue",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"heading_collapsed": true,
"hidden": true
},
"cell_type": "markdown",
"source": "## `%%bash`\n\nLets you run bash in a subprocess"
},
{
"metadata": {
"hidden": true,
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "%%bash\nfor i in a b c;\ndo\necho $i\ndone",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"heading_collapsed": true,
"hidden": true
},
"cell_type": "markdown",
"source": "## `%%latex`\n\nLets you render LaTeX inline"
},
{
"metadata": {
"hidden": true,
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "%%latex\n\\begin{align}\na = \\frac{a}{b} && b = \\frac{1}{3} && c = \\frac{1}{4} \\\\\na && b && c \\\\\n1 && 2 && 3 \\\\\n\\end{align}",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"hidden": true
},
"cell_type": "markdown",
"source": "## `%%HTML`\n\nLets you render inline HTML, including iFrames"
},
{
"metadata": {
"hidden": true,
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "%%HTML\n<img src=\"https://media.giphy.com/media/l1KtYs7ZpeBskCQus/giphy.gif\" />",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"heading_collapsed": true
},
"cell_type": "markdown",
"source": "# Loading External Magics\n\nWith a quick example of R"
},
{
"metadata": {
"hidden": true,
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "%load_ext rmagic",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"hidden": true,
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "%%R\nX <- runif(10)\nY <- runif(10)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"hidden": true,
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "%R plot(X, Y)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"heading_collapsed": true
},
"cell_type": "markdown",
"source": "# Combining Magics\n\nWith a couple quick examples"
},
{
"metadata": {
"heading_collapsed": true,
"hidden": true
},
"cell_type": "markdown",
"source": "## A Simple Example with Bash & Python"
},
{
"metadata": {
"hidden": true,
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "!wget -O - 'http://www.sfgate.com' > sfgate.html",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"hidden": true,
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "%matplotlib inline \n\nimport matplotlib.pyplot as plt\nfrom wordcloud import WordCloud, STOPWORDS\nimport nltk\n\ndata = open(\"sfgate.html\",'r').read()\ntext = nltk.clean_html(data)\ncleaned = nltk.word_tokenize(text.lower())\nwordlist = [x for x in cleaned if (len(x)>=2 and x.isalpha())]\nwordcloud = WordCloud(stopwords=STOPWORDS, background_color='white').generate(\" \".join(wordlist))\nplt.figure(figsize=(15,10))\nplt.imshow(wordcloud)\nplt.axis('off')\nplt.show()",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"hidden": true,
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "!rm 'sfgate.html'",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"heading_collapsed": true,
"hidden": true
},
"cell_type": "markdown",
"source": "## Mixing Languages"
},
{
"metadata": {
"hidden": true,
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "import numpy as np\nX = np.array([4.5, 6.3, 9.1])\nX.mean()",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"hidden": true,
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "%Rpush X\n%R mean(X)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"hidden": true,
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"toc": {
"threshold": 4,
"number_sections": true,
"toc_cell": false,
"toc_window_display": false,
"toc_section_display": "block",
"sideBar": true,
"navigate_menu": true,
"moveMenuLeft": true,
"widenNotebook": false,
"colors": {
"hover_highlight": "#DAA520",
"selected_highlight": "#FFD700",
"running_highlight": "#FF0000",
"wrapper_background": "#FFFFFF",
"sidebar_border": "#EEEEEE",
"navigate_text": "#333333",
"navigate_num": "#000000"
},
"nav_menu": {
"width": "252px",
"height": "228px"
}
},
"language_info": {
"name": "python",
"version": "3.6.0",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"varInspector": {
"window_display": false,
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"library": "var_list.py",
"delete_cmd_prefix": "del ",
"delete_cmd_postfix": "",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"library": "var_list.r",
"delete_cmd_prefix": "rm(",
"delete_cmd_postfix": ") ",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
]
},
"gist": {
"id": "349269bdf4dfaec82af84be1b0a93005",
"data": {
"description": "Jupyter Magics Tutorial",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/349269bdf4dfaec82af84be1b0a93005"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment