{ "cells": [ { "cell_type": "markdown", "source": [ "# Jupyter Notebooks\r\n", "\r\n", "
\r\n", "From Jupyer Notebook: An Introduction\r\n", "

The Jupyter Notebook is an open source web application that you can use to create and share documents that contain live code, equations, visualizations, and text. Jupyter Notebook is maintained by the people at Project Jupyter.

\r\n", "\r\n", "

Jupyter Notebooks are a spin-off project from the IPython project, which used to have an IPython Notebook project itself. The name, Jupyter, comes from the core supported programming languages that it supports: Julia, Python, and R. Jupyter ships with the IPython kernel, which allows you to write your programs in Python, but there are currently over 100 other kernels that you can also use.

\r\n", "
\r\n", "\r\n", "[Anaconda](https://www.anaconda.com/) comes with many scientific libraries preinstalled, including the Jupyter Notebook!" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Interactive Coding!!\n", "We will use Jupyter Notebooks to interface with Python. Rather than keeping code scripts code and execution separate, Jupyter integrates both together using the concept of cells. Two main types of cells are **code cells** and **markdown cells**. Cells pair \"snippets\" of code with the output obtained from running them and can contain plots/figures inline alongside code required to generate them.\n", "\n", "````{panels}\n", "**Code cells** contain actual code that you want to run. You can specify a cell as a code cell using the pulldown menu in the toolbar in your Jupyter notebook. Otherwise, you can can hit esc and then y (denoted \"*esc, y*\") while a cell is selected to specify that it is a code cell. Note that you will have to hit enter after doing this to start editing it. If you want to execute the code in a code cell, hit \"*shift + enter*\". Note that code cells are executed in the order you execute them. That is to say, the ordering of the cells for which you hit \"*shift + enter*\" is the order in which the code is executed. If you did not explicitly execute a cell early in the document, its results are now known to the Python interpreter.\n", "\n", "---\n", "\n", "**Markdown cells** contain text. The text is written in Markdown, a lightweight markup language. You can read about its syntax [here](https://daringfireball.net/projects/markdown/syntax). Note that you can also insert HTML into markdown cells, and this will be rendered properly. As you are typing the contents of these cells, the results appear as text. Hitting \"*shift + enter*\" renders the text in the formatting you specify. You can specify a cell as being a markdown cell in the Jupyter toolbar, or by hitting \"*esc, m*\" in the cell. Again, you have to hit enter after using the quick keys to bring the cell into edit mode.\n", "````\n", "\n", "In general, when you want to add a new cell, you can use the \"Insert\" pulldown menu from the Jupyter toolbar. The shortcut to insert a cell below is \"*esc, b*\" and to insert a cell above is \"*esc, a*\". Alternatively, you can execute a cell and automatically add a new one below it by hitting \"*alt + enter*\"." ], "metadata": {} }, { "cell_type": "code", "execution_count": null, "source": [ "# This is a code cell\r\n", "# Put some code here and get some output below!\r\n", "x = 10\r\n", "print(x)" ], "outputs": [], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Notebooks of the Future\n", "Not just for code:\n", "- Markdown, HTML, LaTeX integration\n", "- Slide shows\n", "- Keep your notes alongside your analysis routines\n", "- Embed images, videos, anything (it's all just HTML + javascript)" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "### Markdown\r\n", "See this [comprehensive guide to Markdown](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) or [tutorial](https://www.markdowntutorial.com/). Here are some examples:\r\n", "\r\n", "#### Emphasis\r\n", "\r\n", "Here is one way to add emphasis to to Markdown cell text.\r\n", "```{tab} Markdown Input 1\r\n", "`Emphasis, aka italics, with *asterisks*.`\r\n", "```\r\n", "```{tab} Markdown Output 1\r\n", "Emphasis, aka italics, with *asterisks*.\r\n", "```\r\n", "\r\n", "Here is another way to add emphasis to to Markdown cell text.\r\n", "```{tab} Markdown Input 2\r\n", "`Strong emphasis, aka bold, with **asterisks**.`\r\n", "```\r\n", "```{tab} Markdown Output 2\r\n", "Strong emphasis, aka bold, with **asterisks**.\r\n", "```\r\n", "\r\n", "Here is another way to add emphasis to to Markdown cell text.\r\n", "```{tab} Markdown Input 3\r\n", "`Combined emphasis with ***asterisks***.`\r\n", "```\r\n", "```{tab} Markdown Output 3\r\n", "Combined emphasis with ***asterisks***.\r\n", "```\r\n", "\r\n", "Here is one way to add strikethough to Markdown cell text.\r\n", "```{tab} Markdown Input 4\r\n", "`Strikethrough uses two tildes. ~~Scratch this.~~`\r\n", "```\r\n", "```{tab} Markdown Output 4\r\n", "Strikethrough uses two tildes. ~~Scratch this.~~\r\n", "```\r\n", "\r\n", "#### Links\r\n", "\r\n", "Here is one way to insert hyperlinks into Markdown cells.\r\n", "```{tab} Markdown Input 5\r\n", "`[I'm an inline-style link](https://www.google.com)`\r\n", "```\r\n", "```{tab} Markdown Output 5\r\n", "[I'm an inline-style link](https://www.google.com)\r\n", "```\r\n", "\r\n", "Here is another way to insert hyperlinks into Markdown cells.\r\n", "```{tab} Markdown Input 6\r\n", "`[I'm a reference-style link][Arbitrary case-insensitive reference text]`\r\n", "```\r\n", "```{tab} Markdown Output 6\r\n", "[I'm a reference-style link][Arbitrary case-insensitive reference text]\r\n", "```\r\n", "\r\n", "[arbitrary case-insensitive reference text]: https://shawnrhoads.github.io/gu-psyc-347/\r\n", "[1]: https://shawnrhoads.github.io/gu-psyc-347/\r\n", "[link text itself]: https://shawnrhoads.github.io/gu-psyc-347/\r\n", "\r\n", "To reference the links above, we include the following text:\r\n", "```\r\n", "[arbitrary case-insensitive reference text]: https://shawnrhoads.github.io/gu-psyc-347/\r\n", "[1]: https://shawnrhoads.github.io/gu-psyc-347/\r\n", "[link text itself]: https://shawnrhoads.github.io/gu-psyc-347/\r\n", "```\r\n", "\r\n", "URLs and URLs in angle brackets will automatically get turned into links. http://www.example.com or and sometimes \r\n", "example.com (but not on Github, for example).\r\n", "\r\n", "#### Tables\r\n", "Colons can be used to align columns (note that this may not render properly in the Jupyter Book website).\r\n", "\r\n", "````{tab} Markdown Input 7\r\n", "```\r\n", "| Tables | are | Cool |\r\n", "| :------------ |:-------------:| -----:|\r\n", "| row | 1 | A |\r\n", "| row | 2 | B |\r\n", "| row | 3 | C |\r\n", "```\r\n", "````\r\n", "````{tab} Markdown Output 7\r\n", "| Tables | Are | Cool |\r\n", "| :------------ |:-------------:| -----:|\r\n", "| row | 1 | A |\r\n", "| row | 2 | B |\r\n", "| row | 3 | C |\r\n", "````\r\n", "\r\n", "The outer pipes (|) are optional, and you don't need to make the raw Markdown pretty. You can also use inline Markdown.\r\n", "\r\n", "````{tab} Markdown Input 8\r\n", "```\r\n", "Markdown | Less | Pretty\r\n", "--- | --- | ---\r\n", "*Still* | `renders` | **nicely**\r\n", "1 | 2 | 3\r\n", "```\r\n", "````\r\n", "````{tab} Markdown Output 8\r\n", "Markdown | Less | Pretty\r\n", "--- | --- | ---\r\n", "*Still* | `renders` | **nicely**\r\n", "1 | 2 | 3\r\n", "````" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "### Inserting images from web" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "Let's include the Jupyter logo below.\r\n", "\r\n", "```{tab} Markdown Input 9\r\n", "`![logo](https://jupyter.org/assets/main-logo.svg)`\r\n", "```\r\n", " \r\n", "```{tab} Markdown Output 9\r\n", "![logo](https://jupyter.org/assets/main-logo.svg)\r\n", "```\r\n" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "### HTML/CSS Integration:\n", "\n", "Here's an example using HTML!\n", "\n", "```{tab} HTML Input\n", "`This is a *markdown* **cell**!`\n", "```\n", "\n", "```{tab} HTML Output\n", "This is a *markdown* **cell**!\n", "```" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "### LaTeX Integration:\n", "\n", "Here's an example using LaTeX\n", "```{tab} LaTeX Input\n", "`$\\sum_{n=1}^{5}n$`\n", "```\n", "\n", "```{tab} LaTeX Output\n", "$\\sum_{n=1}^{5}n$\n", "```" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "### Multilingual!\n", "\n", "Jupyter Notebooks support over [150+ languages](https://github.com/jupyter/jupyter/wiki/Jupyter-kernels)!\n", "\n", "These include:\n", "\n", "- Julia\n", "- Python\n", "- R\n", "- Javascript" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "### Save notebooks in other formats and put them online\n", "- Github automatically renders Jupyter Notebooks\n", "- [Online notebook viewer](http://nbviewer.jupyter.org/)\n", "- Output to PDF, HTML (including JavaScript)\n", "- [JupyterHub](https://github.com/jupyterhub/jupyterhub)\n", "\n", "### Customize your notebook experience with extensions\n", "- Table of Contents\n", "- Execution time/Profiling\n", "- Scratch Space\n", "- Code/Section folding\n", "- Look and feel (CSS+Javascript)\n", "- Other notebook extensions\n", "\n", "### Additional Resources\n", "- [Eshin Jolly - Intro to Jupyter Notebooks](https://github.com/Summer-MIND/mind_2018/tree/master/tutorials/jupyter)\n", "- [Jake Vanderplas - Data Science Handbook](https://github.com/jakevdp/PythonDataScienceHandbook)\n", "- [Tal Yarkoni - Intro to Data Science w/ Python](https://github.com/tyarkoni/SSI2016)\n", "- [Yaroslav Halchenko - Intro to Programming for Psych/Neuro](https://github.com/dartmouth-pbs/psyc161)\n", "- [Awesome notebooks](https://github.com/jupyter/jupyter/wiki/A-gallery-of-interesting-Jupyter-Notebooks)\n", "- [Neuroimaging in Python](https://github.com/datacarpentry/python-neuroimaging-lesson)\n", "- [Google Colaboratory](https://colab.research.google.com/notebooks/welcome.ipynb#recent=true)" ], "metadata": {} } ], "metadata": { "colab": { "collapsed_sections": [] }, "interpreter": { "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" }, "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.9" } }, "nbformat": 4, "nbformat_minor": 4 }