{ "cells": [ { "cell_type": "markdown", "source": [ "# Jupyter Lab Exercises\n", "\n", "In these exercises, we'll explore a few ways of working with Jupyter Lab. If you haven't already, make sure you've worked through the tutorials for installing [Python and Conda](../setting_up_your_computer.ipynb), and for [setting up R](../jupyter.ipynb) to work with Jupyter.\n", "\n", "As we work through these exercises, we'll be openning and working with a set of files you can download [here](https://github.com/nickeubank/practicaldatascience/blob/master/Example_Data/jupyter_lab.zip) called `jupyter_lab.zip`. " ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Replicating an RStudio Interface\n", "\n", "At this point, all MIDS students have worked with RStudio, so let's begin by replicating that familiar environment. If you have never used RStudio and don't want to work with R, feel free to skip this exercise. " ], "metadata": {} }, { "cell_type": "markdown", "source": [ "First, let's open the file `analyze_health_and_income.R` (make sure it's the `.R` file) by navigating to where you downloaded `jupyter_lab` folder using the file navigator on the left hand pane. If the file navigator not immediately visible, you can open it by clicking the folder icon in at the top of the left sidebar. \n", "\n", "This has now opened a text editor window. To pair it with an active kernel (i.e. a running session of R), we need to right click somewhere inside the file and select \"Create Console for Editor\", then choose to open an R kernel. This will open a new window with the same name as your current text file (\"analyze_health.R\"), but with a different icon (there should be a blue box with a cursor).\n", "\n", "This window is your interface to an active R session, and the setup you have now is basically the same as in RStudio:\n", "\n", "![rstudio](../images/rstudio_layout.png)\n", "\n", "You can enter commands directly into the console window, or you can run a block of code from your text-editor window by highlighting it and hitting Shift-Enter. \n", "\n", "### Exercise 1 \n", "\n", "Run `print('hi')` by typing it directly into your console session.\n", "\n", "### Exercise 2 \n", "\n", "Now run the first few lines of \"analyze_health_and_income.R\" by selecting the text and hitting Shift-Enter." ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Replicating Spyder Interface\n", "\n", "I will confess I've never loved having the console below the text file, so let's also explore having them side-by-side (in a scheme that looks like the scheme used by Spyder).\n", "\n", "### Exercise 3\n", "\n", "Grab the tab of the `analyze_health_and_income` console tab and drag it to the right side of the text window: \n", "\n", "![converting_to_spyder](../images/converting_to_spyder.png)\n", "\n", "Then you'll get something like this:\n", "\n", "![spyder_layout](../images/spyder_layout.png)" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Multiple Kernels\n", "\n", "### Exercise 4\n", "\n", "Now let's open `analyze_health_and_income.py` (note the `py` file suffix: this is a Python file not R file). Create a kernel for this file as well by right clicking on the text file and clicking \"Create A Console for Editor\". This time select Python 3.\n", "\n", "(By default the new console will appear along the bottom, so move it on top of the other console to organize your space:\n", "\n", "![moving_second_console](../images/moving_second_console.png)\n", "\n", "![two_tabs](../images/two_tabs.png)" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "### Exercise 5\n", "\n", "Now run your Python code by highlighting text and hitting shift return! Look at that, R and Python, side by side. \n", "\n", "(Note that this code uses a package called `plotnine`, so you may need to first install it. At the command line, run `pip install plotnine`. We'll talke more about installing packages in a later lesson). \n", "\n", "One quirk of Jupyter Lab is that because you can have many open consoles, each text file will only send code to the console specifically created to work with that file. This is different from RStudio, where all executed code runs in one open kernel, and from Spyder, where code runs in whatever session is currently the front-most tab. " ], "metadata": {} }, { "cell_type": "markdown", "source": [ "### Exercise 6" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "Jupyter Lab can also be used to manage your Jupyter Notebooks. In order to get some experience working with a Jupyter Notebook, lets move the analysis we do in this `analyze_health_and_income.py` into a Jupyter Notebook.\n", "\n", "First, download the starter notebook for this exercise [here](https://github.com/nickeubank/practicaldatascience/blob/master/Example_Data/first_jupyter_notebook.ipynb). (Downloading jupyter notebooks from github can be a little tricky. Click `Raw`, then right click and select `Save Page As...`, then save the file as `first_jupyter_notebook.ipynb`.) \n", "\n", "**Note:** All jupyter notebooks carry the file suffix of `.ipynb`, regardless of what kernel they are running. This is a hangover from the old days when \"jupyter\" was \"ipython\", so all notebooks were IPython Notebooks. But we still use the suffix `.ipynb` even if your notebook is running a non-Python kernel (like R, Julia, etc.). \n", "\n", "Once downloaded, open the file in Jupyter Lab. " ], "metadata": {} }, { "cell_type": "markdown", "source": [ "### Exercise 7\n", "\n", "Once open, follow the directions in the notebook to complete the exercise!" ], "metadata": {} } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }