Hello World! – running your first Python code
Now that we have a better understanding of the dashboard and its navigation, let's create our first notebook and run some Python code. The easiest method is to click on the New button and select Python 3 in the submenu. This will open a new tab or window in your browser that looks similar to the following screenshot:
I recommend renaming the Untitled files of any notebook to make it easier to find them later. To do this, select Rename from the File menu, as shown in the following screenshot, and rename it hello_world or a relevant project name. Once you click on the OK button, the title bar at the top of the page will display the new name:
By renaming the notebook, a new file with the .ipynb extension will be created, which contains all of the contents in JSON format. This helps make the notebook file shareable to others and helps with version control, which is an audit trail of changes made in the file.
You can view the actual JSON metadata contents by selecting Edit Notebook Metadata from the Edit menu. The results will look similar to the following screenshot:
The UI for the notebook looks very similar to other modern web software used today as it was designed for easy navigation. The following menu options are easy to use icons that are collectively called the Notebook Toolbar, which supports keyboard shortcuts for optimal workflow as you get more comfortable using the tool. You can find the User Interface Tour and Keyboard Shortcuts in the Help menu, as shown in the following screenshot. I recommend going through them to take a look at all of the features available:
Once you feel comfortable with the Help menu options, let's write your first code by typing in the print("hello world")commandin the notebook cell, which has a default ofIn []:. Remember, if you are using a mouse to navigate the notebook, you must click on the cell to select it and have a cursor appear.
Pressing the Enter key after a command will only create a second line for more input. You must either use a keyboard shortcut, the Cell menu, or a Toolbar icon to execute any command.
Once you have entered the print("hello world")commandin the cell and clicked on any one of the following options. The options to run the command in any cell are as follows:
- Click the button from the toolbar.
- Select Run Cells from the Cell menu.
- Press the Shift + Enter or Ctrl + Enter keys.
The screen should look similar to the following screenshot:
Congratulations, you have created your first Jupyter notebook and run your first command! Click on the Close and Halt option from the File menu to return to the dashboard.
Creating a project folder hierarchy
Now that we have covered the basics, let's walk through a directory to find a particular file and create a project folder hierarchy to prepare for future data analysis learning modules. I recommend creating a starting projects folder on your workstation to keep all of your notebooks and data organized. A standard enterprise directory structure will vary by company but setting up a basic structure with subfolders makes the process portable and helps with sharing work with others. An example project folder template is shown in the following screenshot:
In the classic do as I say, not as I do fashion and because of limitations with relative paths across different OS versions, the examples use the same folder to prevent errors throughout this book. To proceed, you can either clone or download all of the files and subfolders from this book's GitHub repository, create all of the folders and files in the Jupyter dashboard, or create them on your workstation. Once completed, your project folder for this chapter should look like the following screenshot:
Uploading a file
Now that we have a project folder, let's walk through the following steps to upload a file for analysis. You must download the file ahead of time from the GitHub repository URL found in the Technical requirements section:
- Click on the data folder name.
- Click on the source subfolder name.
- Click the Upload button at the top-right side of the screen.
- Select evolution_of_data_analysis.csv.
- Click the blue Upload button to proceed. Once done, you'll see a file in the dashboard, as shown in the following screenshot:
- Navigate back to the notebooks folder and create a new notebook file by clicking on the New menu. Similar to the hello_world example, select Python 3 in the submenu to create a default Untitled notebook.
As mentioned earlier, I always rename the Untitled notebook before moving forward, so rename the notebook evolution_of_data_analysis.
- To read data from a file in the notebook, you must run a few Python commands. These can be run all in one cell or as three separate cell entries. The commands to open the CSV file we uploaded earlier are as follows:
f = open("../data/source/evolution_of_data_analysis.csv","r")
print(f.read())
f.close()
Let's walk through the commands line by line. First, we assigned an open command value of the file to the fvariable to shorten the length of additional commands in the next few lines. Notice the evolution_of_data_analysis.csv file includes the directory path of "../data/source/", which is required because the location of the active notebook, evolution_of_data_analysis, is in a different folder. The open command also includes a parameter of r, which means we only want to read the file and not edit the content.
The second line is to print the contents of the file by passing the fvariable along with the read() function. This will display the results in the output cell, which will be similar to the following screenshot:
The last line is a best practice to close the file to avoid any conflicts using the file later or in the filesystem of your OS. Once you verify that you can see the contents of the CSV file in your notebook, click on the Close and Halt option from the File menu to return to the dashboard.