官术网_书友最值得收藏!

Exploring some of Jupyter's most useful features

  1. From the Jupyter Dashboard, navigate to the chapter-1 directory and open the chapter-1-workbook.ipynb file by selecting it. The standard file extension for Jupyter Notebooks is .ipynb, which was introduced back when they were called IPython Notebooks.
  2. Scroll down to Subtopic Jupyter Features in the Jupyter Notebook. We start by reviewing the basic keyboard shortcuts. These are especially helpful to avoid having to use the mouse so often, which will greatly speed up the workflow. Here are the most useful keyboard shortcuts. Learning to use these will greatly improve your experience with Jupyter Notebooks as well as your own efficiency:
    • Shift + Enter is used to run a cell
    • The Esc key is used to leave a cell
    • The M key is used to change a cell to Markdown (after pressing Esc)
    • The Y key is used to change a cell to code (after pressing Esc)
    • Arrow keys move cells (after pressing Esc
    • The Enter key is used to enter a cell

Moving on from shortcuts, the help option is useful for beginners and experienced coders alike. It can help provide guidance at each uncertain step.

Users can get help by adding a question mark to the end of any object and running the cell. Jupyter finds the docstring for that object and returns it in a pop-out window at the bottom of the app.

  1. Run the Getting Help section cells and check out how Jupyter displays the docstrings at the bottom of the Notebook. Add a cell in this section and get help on the object of your choice: 

Tab completion can be used to do the following:

    • List available modules when importing external libraries
    • List available modules of imported external libraries
    • Function and variable completion

This can be especially useful when you need to know the available input arguments for a module, when exploring a new library, to discover new modules, or simply to speed up workflow. They will save time writing out variable names or functions and reduce bugs from typos. The tab completion works so well that you may have difficulty coding Python in other editors after today!

  1. Click into an empty code cell in the Tab Completion section and try using tab completion in the ways suggested immediately above. For example, the first suggestion can be done by typing import (including the space after) and then pressing the Tab key:

  1. Last but not least of the basic Jupyter Notebook features are magic commands. These consist of one or two percent signs followed by the command. Magics starting with %% will apply to the entire cell, and magics starting with % will only apply to that line. This will make sense when seen in an example.

Scroll to the Jupyter Magic Functions section and run the cells containing %lsmagic and %matplotlib inline:

%lsmagic lists the available options. We will discuss and show examples of some of the most useful ones. The most common magic command you will probably see is %matplotlib inline, which allows matplotlib figures to be displayed in the Notebook without having to explicitly use plt.show().

The timing functions are very handy and come in two varieties: a standard timer (%time or %%time) and a timer that measures the average runtime of many iterations (%timeit and %%timeit).

  1. Run the cells in the Timers section. Note the difference between using one and two percent signs.

Even by using a Python kernel (as you are currently doing), other languages can be invoked using magic commands. The built-in options include JavaScript, R, Pearl, Ruby, and Bash. Bash is particularly useful, as you can use Unix commands to find out where you are currently (pwd), what's in the directory (ls), make new folders (mkdir), and write file contents (cat / head / tail).

  1. Run the first cell in the Using bash in the notebook section. This cell writes some text to a file in the working directory, prints the directory contents, prints an empty line, and then writes back the contents of the newly created file before removing it:

  1. Run the following cells containing only ls and pwd. Note how we did not have to explicitly use the Bash magic command for these to work.

There are plenty of external magic commands that can be installed. A popular one is ipython-sql, which allows for SQL code to be executed in cells.

  1. If you've not already done so, install ipython-sql now. Open a new terminal window and execute the following code:
  pip install ipython-sql

  1. Run the %load_ext sql cell to load the external command into the Notebook:

This allows for connections to remote databases so that queries can be executed (and thereby documented) right inside the Notebook.

  1. Run the cell containing the SQL sample query:

Here, we first connect to the local sqlite source; however, this line could instead point to a specific database on a local or remote server. Then, we execute a simple SELECT to show how the cell has been converted to run SQL code instead of Python.

  1. Moving on to other useful magic functions, we'll briefly discuss one that helps with documentation. The command is %version_information, but it does not come as standard with Jupyter. Like the SQL one we just saw, it can be installed from the command line with pip.

If not already done, install the version documentation tool now from the terminal using pipOpen up a new window and run the following code:

pip install version_information

Once installed, it can then be imported into any Notebook using %load_ext version_information. Finally, once loaded, it can be used to display the versions of each piece of software in the Notebook.

  1. Run the cell that loads and calls the version_information command:

主站蜘蛛池模板: 高州市| 香格里拉县| 来宾市| 九台市| 汉中市| 黄浦区| 凭祥市| 定结县| 婺源县| 永丰县| 惠东县| 泾川县| 衡水市| 塔河县| 兴隆县| 明水县| 龙井市| 富锦市| 饶阳县| 北辰区| 白银市| 剑阁县| 平阳县| 铜鼓县| 石家庄市| 平乡县| 云林县| 吴旗县| 濮阳市| 宜良县| 集贤县| 志丹县| 天台县| 神池县| 会泽县| 新河县| 乐安县| 屏东市| 蒙阴县| 永宁县| 双牌县|