序章

Jupyter Notebook is an open-source web application that lets you create and share interactive code, visualizations, and more. このツールは、Python、Julia、R、Haskell、Rubyなどのいくつかのプログラミング言語で使用できます。 多くの場合、データの操作、統計モデリング、機械学習に使用されます。

This tutorial will walk you through setting up Jupyter Notebook to run from an Ubuntu 18.04 server, as well as teach you how to connect to and use the Notebook. Jupyter Notebooks (or simply Notebooks) are documents produced by the Jupyter Notebook app which contain both computer code and rich text elements (paragraph, equations, figures, links, etc.) which aid in presenting and sharing reproducible research. In the final step of this guide, you will run Python 3 code using a Jupyter Notebook running on a remote server.

前提条件

In order to complete this guide, you will need an Ubuntu 18.04 server instance with a firewall and a non-root user with sudo privileges configured. You can set this up by running through our initial server setup tutorial.

Step 1 — Setting Up Python

To begin the process, install the dependencies for your Python programming environment from the Ubuntu repositories. Ubuntu 18.04 comes preinstalled with Python 3.6.9. You will use the Python package manager pip to install additional components later.

First, update the local apt package index:

  1. sudo apt update

次に、Jupyterの依存関係の一部で使用されるpipとPythonヘッダーファイルをインストールします。

  1. sudo apt install python3-pip python3-dev

You can now move on to setting up a Python virtual environment.

Step 2 — Creating a Python Virtual Environment for Jupyter

With some dependencies installed, you can create a Python virtual environment to manage your projects. Jupyter will be installed into this virtual environment.

You will need access to the virtualenv command which can be installed with pip.

First, upgrade pip by typing:

  1. sudo -H pip3 install --upgrade pip

Then install virtualenv with this command:

  1. sudo -H pip3 install virtualenv

The -H フラグは、セキュリティポリシーが home ターゲットユーザーのホームディレクトリへの環境変数。

virtualenv installed, you can start forming your environment. Create and move into a directory where you can keep your project files. The following example directory is called my_project_dir. Use a name that is meaningful for you and what you’re working on.

Create your directory by typing the following command:

  1. mkdir ~/my_project_dir

Move into the my_project_dir with this command:

  1. cd ~/my_project_dir

Within the project directory, create a Python virtual environment. This tutorial will refer to the environment as my_project_env but, again, you can call it something that is relevant to your project.

  1. virtualenv my_project_env

This creates a directory called my_project_env あなたの中で my_project_dir ディレクトリ。 内部には、ローカルバージョンのPythonとローカルバージョンのpipがインストールされます。 You can use this to install and configure an isolated Python environment for Jupyter.

Before installing Jupyter, activate the virtual environment. 次のように入力すると、次のように入力できます。

  1. source my_project_env/bin/activate

Your prompt should change to indicate that you are now operating within a Python virtual environment similar to this:

これで、Jupyterをこの仮想環境にインストールする準備が整いました。

Step 3 — Installing Jupyter

仮想環境をアクティブにして、pipのローカルインスタンスを使用してJupyterをインストールします。

注:仮想環境がアクティブ化されたとき(プロンプトが (my_project_env) その前に)、使用する pip それ以外の pip3、Python3を使用している場合でも。 ツールの仮想環境のコピーには常に名前が付けられます pip、Pythonのバージョンに関係なく。

  1. pip install jupyter

この時点で、Jupyterの実行に必要なすべてのソフトウェアが正常にインストールされています。 You can now start the Notebook server.

Step 4 — Running Jupyter Notebook

You now have everything you need to run Jupyter Notebook. 実行するには、次のコマンドを実行します。

  1. jupyter notebook

JupyterNotebookのアクティビティのログが端末に印刷されます。 Jupyter Notebookを実行すると、特定のポート番号で実行されます。 最初に実行するノートブックは通常、ポートを使用します 8888. Jupyter Notebookが実行されている特定のポート番号を確認するには、JupyterNotebookを起動するために使用したコマンドの出力を参照してください。

Output
[I 21:23:21.198 NotebookApp] Writing notebook server cookie secret to /home/sammy/.local/share/jupyter/runtime/notebook_cookie_secret [I 21:23:21.361 NotebookApp] Serving notebooks from local directory: /home/sammy/my_project_dir [I 21:23:21.361 NotebookApp] The Jupyter Notebook 6.4.10 is running at: [I 21:23:21.361 NotebookApp] http://localhost:8888/?token=1fefa6ab49a498a3f37c959404f7baf16b9a2eda3eaa6d72 [I 21:23:21.361 NotebookApp] or http://127.0.0.1:8888/?token=1fefa6ab49a498a3f37c959404f7baf16b9a2eda3eaa6d72 [I 21:23:21.361 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [W 21:23:21.361 NotebookApp] No web browser found: could not locate runnable browser. [C 21:23:21.361 NotebookApp] To access the notebook, open this file in a browser: file:///home/sammy/.local/share/jupyter/runtime/nbserver-7924-open.html Or copy and paste one of these URLs: http://localhost:8888/?token=1fefa6ab49a498a3f37c959404f7baf16b9a2eda3eaa6d72 or http://127.0.0.1:8888/?token=1fefa6ab49a498a3f37c959404f7baf16b9a2eda3eaa6d72

サーバーではなくローカルコンピューターでJupyterNotebookを実行している場合は、表示されたURLに移動してJupyterNotebookに接続できます。 サーバーでJupyterNotebookを実行している場合は、次のセクションで概説するように、SSHトンネリングを使用してサーバーに接続する必要があります。

At this point, you can keep the SSH connection open and keep Jupyter Notebook running or you can exit the app and re-run it once you set up SSH tunneling.

If you would like to stop the Jupyter Notebook process, press CTRL + C、 タイプ Y、 その後 ENTER 確認するために。 次の出力が表示されます。

Output
[C 19:23:34.939 NotebookApp] Shutdown confirmed [I 19:23:34.940 NotebookApp] Shutting down 0 kernels [I 19:23:34.941 NotebookApp] Shutting down 0 terminals

You can now set up an SSH tunnel to access the Notebook.

Step 5 — Connecting to the Server Using SSH Tunneling

Jupyter Notebook runs on a specific port on the server (such as :8888, :8889 etc.). SSH tunneling enables you to connect to the server’s port securely.

The next two subsections describe how to create an SSH tunnel from a Mac, Linux, and Windows operating system. ローカルコンピュータのサブセクションを参照してください。

MacまたはLinuxでのSSHトンネリング

MacまたはLinuxを使用している場合、SSHトンネルを作成する手順は、SSHを使用してリモートサーバーにログインする手順と似ていますが、 ssh 指図。

SSHトンネリングは、新しいローカルターミナルウィンドウで次のSSHコマンドを実行することで実行できます。

  1. ssh -L 8888:localhost:8888 your_server_username@your_server_ip

The ssh コマンドはSSH接続を開きますが、 -L ローカル(クライアント)ホストの指定されたポートが、リモート側(サーバー)の指定されたホストおよびポートに転送されることを指定します。 これは、2番目のポート番号で実行されているものはすべて(例: 8888)サーバー上で最初のポート番号に表示されます(例: 8888)ローカルコンピュータ上。

オプションでポートを変更します 8888 別のプロセスですでに使用されているポートの使用を避けるために選択したものの1つに。

server_username あなたのユーザー名です(例: 作成したサーバー上のsammy)と your_server_ip is the IP address of your server.

たとえば、ユーザー名の場合 sammy およびサーバーアドレス 203.0.113.0、コマンドは次のようになります。

  1. ssh -L 8888:localhost:8888 sammy@203.0.113.0

If no error messages after running the ssh -L コマンドを使用すると、プログラミング環境に移動してJupyterNotebookを実行できます。

  1. jupyter notebook

URL付きの出力を受け取ります。 ローカルマシンのWebブラウザから、JupyterNotebookWebインターフェイスを次のURLで開きます。 http://localhost:8888. トークン番号が含まれていることを確認するか、プロンプトが表示されたらトークン番号の文字列を入力します http://localhost:8888:

Output
... [I 21:23:21.361 NotebookApp] The Jupyter Notebook 6.4.10 is running at: [I 21:23:21.361 NotebookApp] http://localhost:8888/?token=1fefa6ab49a498a3f37c959404f7baf16b9a2eda3eaa6d72 ...

WindowsとPuttyを使用したSSHトンネリング

Windowsを使用している場合は、Puttyを使用してSSHトンネルを作成できます。

First, enter the server URL or IP address in the Host Name (or IP address) field as shown:

次に、左ペインの下部にある SSH をクリックしてメニューを展開し、トンネルをクリックします。 ローカルマシンでJupyterにアクセスするために使用するローカルポート番号を入力します。 選ぶ 8000 他のサービスによって使用されるポートを回避し、宛先を次のように設定するには、またはそれ以上 localhost:8888 どこ :8888 JupyterNotebookが実行されているポートの番号です。

Click the Add button, and the ports should appear in the Forwarded ports list:

最後に、 Open ボタンをクリックして、SSH経由でサーバーに接続し、目的のポートをトンネリングします。 案内する http://localhost:8000 (or the port you chose) in a web browser to connect to Jupyter Notebook running on the server. トークン番号が含まれていることを確認するか、プロンプトが表示されたらトークン番号の文字列を入力します http://localhost:8000.

Whether you’re connecting within a Mac/Linux OS or Windows, you should now be connected to your server via SSH tunneling. You’re now ready to use Jupyter Notebook in your web browser.

ステップ6—JupyterNotebookを使用する

現在JupyterNotebookを実行していない場合は、 jupyter notebook command:

  1. jupyter notebook

これで、Webブラウザを使用して接続する必要があります。 Jupyter Notebookは、多くの機能を備えた非常に強力なツールです。 このセクションでは、ノートブックの使用を開始するためのいくつかの基本機能の概要を説明します。 Jupyter Notebookは、実行元のディレクトリ内のすべてのファイルとフォルダーを表示するため、プロジェクトで作業しているときは、必ずプロジェクトディレクトリから開始してください。

To create a new Notebook file, select New > Python 3 from the top right drop-down menu:

これにより、ノートブックが開きます。 You can now run Python code in the cell or change the cell to markdown. たとえば、上部のナビゲーションバーからセル>セルタイプ>マークダウンをクリックして、最初のセルを変更してマークダウンを受け入れます。 You can now write notes using Markdown and even include equations written in LaTeX by putting them between the $$ シンボル。 たとえば、セルをマークダウンに変更した後、セルに次のように入力します。

# First Equation

Let us now implement the following equation:
$$ y = x^2$$

where $x = 2$

マークダウンをリッチテキストに変換するには、を押します CTRL + ENTER、および次の結果が得られるはずです。

マークダウンセルを使用して、メモを作成し、コードを文書化できます。 To implement that equation and print the result, click on the top cell, then press ALT + ENTER その下にセルを追加します。 新しいセルに次のコードを入力します。

x = 2
y = x**2
print(y)

コードを実行するには、を押します CTRL + ENTER. 次の結果が表示されます。

You now have the ability to import modules and use the Notebook as you would with any other Python development environment.

結論

これで、Jupyter Notebookを使用して、再現可能なPythonコードとメモをMarkdownで記述できるようになります。 インターフェイス内からJupyterNotebookのクイックツアーを取得するには、上部のナビゲーションメニューからヘルプ>ユーザーインターフェイスツアーを選択して詳細を確認してください。

ここから、Python3パンダとJupyterNotebookを使用したデータ分析と視覚化を読むことでデータ分析と視覚化プロジェクトを開始できます。

さらに詳しく知りたい場合は、時系列の視覚化と予測に関するシリーズを読むことができます。