{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###### The cell below loads the style of this notebook." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.core.display import HTML\n", "css_file = '../styles.css'\n", "HTML(open(css_file, \"r\").read())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# A brief introduction to the Jupyter Notebook\n", "-------------------------------------------------------------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "

Learning Objectives

\n", "
\n", "
\n", "\n", "> - become familiar with the notebook interface\n", "> - know how to **run code** and **get help** in the notebook\n", "> - learn how to **format text**, **display images** in the notebook\n", "> - learn how to present **equations** in the notebook\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Notebook Basics\n", "\n", "## The notebook dashboard\n", "\n", "We've already seen how to start the notebook server. When you first start the notebook server, your browser will open to the notebook dashboard. The dashboard serves as a home page for the notebook. Its main purpose is to display the notebooks and files in the **current directory**. When you first start the server, it will show the directory in which you started the notebook server.\n", "\n", "\n", "\n", "To create a new notebook, click on the \"New\" button at the top of the list and select a notebook type from the dropdown (as seen below). Which types are listed depend on what's installed on the server. Some of the notebook types in the screenshot below may not exist as an option to you. \n", "\n", "\n", "\n", "Notebooks and files can be uploaded to the current directory by dragging a notebook file onto the notebook list or by the \"upload\" text above the list.\n", "\n", "The notebook list shows green \"Running\" text and a green notebook icon next to running notebooks (as seen below). **Notebooks remain running until you explicitly shut them down; closing the notebook's page is not sufficient.** To see all of your running notebooks along with their directories, click on the \"Running\" tab: \n", "\n", "\n", "\n", "You can shutdown notebooks from here. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "

Exercise

\n", "
\n", "
\n", "\n", "> Start the notebook server, and create a new notebook so you can play along with this tutorial.\n", "\n", "---\n", "\n", "# The notebook UI\n", "---\n", "\n", "## Cell types\n", "The notebook is composed of cells, which you can add, delete, edit and move around. Each cell can be a particular **type**. The two you really need are *code* cells and *markdown* cells. Code cells should be self-explanatory, but markdown cells are used to create formatted text - like the text you're reading now.\n", "\n", "The cell type is controlled by the drop-down box in the toolbar.\n", "\n", "## Run code\n", "\n", "This is the most important bit! In the notebook, to run a cell of code, hit Shift-Enter or press the button in the toolbar above. This executes the cell and puts the cursor in the next cell below, or makes a new one if you are at the end. Alternately, you can use:\n", "\n", "* Alt-Enter to always add a new cell below\n", "* Ctrl-Enter to run the cell and keep the cursor in the cell." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n" ] } ], "source": [ "print(\"hello\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get Help\n", "\n", "Typing ```some_thing?``` will print out detailed help about that thing, and is a really nice way of getting help on how to use libraries you may import, e.g." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import math\n", "math.sin?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also get help by typing ```help(some_thing)```. Unlike the example above, this will display the help in the notebook itself, e.g." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function sin in module math:\n", "\n", "sin(...)\n", " sin(x)\n", " \n", " Return the sine of x (measured in radians).\n", "\n" ] } ], "source": [ "help(math.sin)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tab completion\n", "\n", "A very nice feature of the notebook is that if you begin to type the name of something and hit Tab, it will complete the name of that object. This also works for members of a library, or for file and directory names." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "math." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", ">Notice how when I run cells a little number pops up next to them? That keeps track of which cell was executed when. Behind the notebook is an instance of the IPython interpreter, which runs your code and keeps track of what you've done. Thus, the following works:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = 3\n", "y = 2" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "print(x+y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> In the notebook you can run cells *out of order*, or run cells again!. This can produce very confusing results. If your notebook gets into such a confused state, it can be useful to stop the IPython interpreter (hit the button in the toolbar) and run the cells again in order.\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Other Stuff\n", "---\n", "\n", " - There are useful commands in the ```Edit``` menu for cutting, pasting and moving cells around.\n", " - Extensive help, for the notebook and Python more generally, is in the ```Help``` menu, unsuprisingly." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "

Exercise: getting to know the notebook

\n", "
\n", "
\n", "\n", "> Find a notebook which grabs your interest from the [Jupyter notebook gallery](https://github.com/ipython/ipython/wiki/A-gallery-of-interesting-IPython-Notebooks) and download it by clicking on the top-right download icon (). Then import it into your running Notebook server using the Dashboard.\n", "\n", "> *(If you are using the Astro Python Server, just open the notebook named example*.) \n", "\n", "> Experiment with running cells in the notebook. Then add at least one cell of each type (code and markdown) to the end of the notebook. Practice the following cell operations:\n", "\n", "> * moving cells up and down;\n", "> * cut/copy/paste cells;\n", "> * merging and splitting cells." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "
\n", "
\n", "

Conclusions

\n", "
\n", "
\n", "\n", "> - we've learned how to run the notebook server and create new notebooks\n", "> - we've learned how to move and edit cells\n", "> - we've learned how to run code cells and get help\n", "\n", "> Next, we'll look at using markdown cells to produce formatted text, equations and display images." ] } ], "metadata": { "kernelspec": { "display_name": "IPython (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.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }