How to remotely access the Jupyter notebook on the intranet from the public network and run code anytime and anywhere!

Preface

Jupyter Notebook is an interactive data science and computing environment that supports multiple programming languages ​​such as Python, R, Julia, etc. It has wide applications in fields such as data science, machine learning, deep learning, education, and document writing.

The important thing is that Jupyter Notebook can be used in a web browser, so we can start the service on the server side, access the code remotely through the web page for data analysis, and have a scientific computing environment that can be used anytime and anywhere without having to install it locally. Configure the Python environment.

1. Python environment installation

Install python environment in windows

https://www.python.org/downloads/windows/

image-20230223134015977

2. Jupyter installation

Open the cmd command line and enter:

pip install jupyter

image-20230223154418341

3. Start Jupyter Notebook

Jupyter Notebook is an open source web application that allows you to create and share documents containing live code, equations, visualizations, and text. It is also a derivative project of the IPython project. The IPython project itself once had an IPython Notebook project. Later it was separated from it and called Jupyter Notebook.

Start Jupyter Notebook

jupyter notebook

image-20230223155413053

After startup, it will automatically jump to the browser opening interface. If there is no jump, just enter the IP address that appears in the window.

image-20230223155527691

4. Remote access

Here we use cpolar for intranet penetration, which supports http/https/tcp protocols, does not limit traffic, does not require a public IP address, and does not require setting up a router. It is simple to use.

4.1 Install and configure cpolar intranet penetration

cpolar official website: https://www.cpolar.com/

Visit the cpolar official website, register an account, and then download and install the client. For specific installation instructions, please refer to the official website documentation tutorial.

  • Windows system: After downloading the installation package from the official website, double-click the installation package and install it by default.
  • Linux system: supports one-click automatic installation script. For details, please refer to the official website documentation - Getting Started Guide

20230130105715

4.2 Create tunnel mapping local port

After cpolar is successfully installed, access the local 9200 port [ http://localhost:9200 ] on the browser and log in using the cpolar account.

20230130105810

Click Tunnel Management on the left dashboard - Create Tunnel, create an 8888 port http tunnel, because jupyter notebook defaults to port 8888

  • Tunnel name: You can customize the name. Be careful not to duplicate the existing tunnel name.
  • Protocol: Select http
  • Local address: 8888
  • Domain name type: Choose a random domain name for free
  • Region: Select China vip

Click创建

image-20230223162138325

After the tunnel is successfully created, click Status on the left - Online Tunnel List, view the generated public network address, and then copy the address

image-20230223162622583

Open the browser, we use the above public network address to connect, the following problem will occur, this problem is that jupyter notebook restricts ip access

image-20230223164122217

Solution:

First close the cmd running window, open a new window, execute the following command to generate a configuration file, remember the configuration file path

jupyter notebook --generate-config

image-20230223165229828

Set access password

jupyter notebook password

image-20230223171005798

Enter the json file path above, open the jupyter_notebook_config.json configuration file, and copy the ciphertext

image-20230223171135988

Then open the jupyter_notebook_config.py configuration file above, add the following parameters, and save after adding

c.NotebookApp.ip='*' #允许访问的IP地址,设置为*代表允许任何客户端访问
c.NotebookApp.password = u'argon2:$argon2id$v=19$m=10240,t=10,p=8$QcZlyMBu9icwhDDiUl+lZw$y4EP3WI4gZjc'#刚才生成密码时上面复制的密文
c.NotebookApp.open_browser = False
c.NotebookApp.port =8888 #可自行指定一个端口, 访问时使用该端口
c.NotebookApp.allow_remote_access = True

image-20230223172907811

Then cmd starts again

image-20230223173219974

Open the browser, we use the above public network address to connect again, the password appears, enter the password set above to log in

image-20230223173324383

5. Fixed public network address

Since the tunnel created using cpolar above uses a random public network address, it will change randomly within 24 hours, which is not conducive to long-term remote access. Therefore, we can configure a second-level subdomain name for it. This address is a fixed address and will not change randomly [ps: cpolar.cn has been filed]

Note that you need to upgrade the cpolar package to a basic package or above, and the bandwidth corresponding to each package is different. [cpolar.cn has been registered]

Reserve a second-level subdomain

Log in to the cpolar official website, click Reserve on the left, select to reserve the second-level subdomain name, set a second-level subdomain name, click Reserve, and copy the reserved second-level subdomain name after the reservation is successful.

image-20230223173929959

After the reservation is successful, copy the reserved second-level subdomain name address

image-20230223174355899

Visit http://127.0.0.1:9200/, log in to the cpolar web UI management interface, click Tunnel Management - Tunnel List on the left dashboard, find the 8888 tunnel you want to configure, and click Edit on the right

image-20230223174657488

Modify the tunnel information and configure the successfully reserved second-level subdomain name into the tunnel.

  • Domain name type: Select a second-level subdomain name
  • Sub Domain: Fill in the successfully reserved second-level subdomain name

Click更新

image-20230223174932054

After the update is completed, open the online tunnel list. At this time, you can see that the public network address has changed and the address name has become the reserved second-level subdomain name. Copy it.

image-20230223175119849

Then use the fixed http address to open the browser for access, the login is successful, and the access is successful.

image-20230223175542675

Reprinted from cpolar pole cloud article: Remote Jupyter Notebook, remote access to the intranet Jupyter server from the public network [intranet penetration]

Guess you like

Origin blog.csdn.net/CSDN_anhl/article/details/133001052