How to run JavaScript in Jupyter Notebook

How to run JavaScript in Jupyter Notebook

Later, I also added how to use Jupyter Notebook in VSC...

Install Anaconda

For the installation part, I used Anaconda directly. You just need to download the msi file. It is not difficult.

Encountering errors and configuring Anaconda

The error message is as follows:

D:\>jupyter notebook
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\Scripts\jupyter-notebook-script.py", line 6, in <module>
from notebook.notebookapp import main
File "C:\ProgramData\Anaconda3\lib\site-packages\notebook\notebookapp.py", line 49, in <module>
from zmq.eventloop import ioloop
File "C:\ProgramData\Anaconda3\lib\site-packages\zmq\__init__.py", line 47, in <module>
from zmq import backend
File "C:\ProgramData\Anaconda3\lib\site-packages\zmq\backend\__init__.py", line 40, in <module>
reraise(*exc_info)
File "C:\ProgramData\Anaconda3\lib\site-packages\zmq\utils\sixcerpt.py", line 34, in reraise
raise value
File "C:\ProgramData\Anaconda3\lib\site-packages\zmq\backend\__init__.py", line 27, in <module>
_ns = select_backend(first)
File "C:\ProgramData\Anaconda3\lib\site-packages\zmq\backend\select.py", line 28, in select_backend
mod = __import__(name, fromlist=public_api)
File "C:\ProgramData\Anaconda3\lib\site-packages\zmq\backend\cython\__init__.py", line 6, in <module>
from . import (constants, error, message, context,
ImportError: DLL load failed: The specified module could not be found.

The most worrying thing is The specified module could not be found , but it requires downloading additional DDL - downloading unofficial DDL is never a solution to the problem.

Later I looked for it again and found that in this case, Anaconda is usually not configured properly: just add the bin in the Library under Anaconda to the PATH.

Taking the above error message as an example, the corresponding bin folder is in C:\ProgramData\Anaconda3\Library\bin .

Configuring Anaconda in Windows is not very user-friendly. You need to manually add some changes to PATH. Take my installation directory C:\ProgramData\Anaconda3 as an example:

set PATH=%PATH%;C:\ProgramData\Anaconda3;C:\Anaconda3\ProgramData\Scripts\

Or manually find System Environment Variables and update them.

At this point, there should be three paths related to Anaconda in PATH:

  • C:\ProgramData\Anaconda3
  • C:\Anaconda3\ProgramData\Scripts\
  • C:\ProgramData\Anaconda3\Library\bin

After the configuration is completed, try running conda in CMD. If it works, it means that most of the functions can be used in CMD, and there is no need to open Conda Terminal separately. Try running jyputer again and the error message should disappear.

If the configuration does not solve the problem, you have to try reinstalling.

Run Jupyter Notebook in the specified folder

Switch to the specified location in CMD and run Jupyter Notebook. Take drive D as an example:

> pushd D:\
# Just run Jupyter Notebook> jupyter notebook

Jupyter Notebook Install JavaScript Environment

There is a package on npm: ijavascript.

ijavascript is described as a tool for running JavaScript core on Jupyter Notebook. The installation method is also very simple. Take Anaconda as an example:

conda install nodejs
npm install -g ijavascript
ijsinstall

In addition to the Anaconda version, it also supports Windows natively, MacOS and Linux.

After the installation is complete and you run it again, you can see that Jupyter Notebook can now be opened to create a new NodeJS notebook environment:

jupyter-js

Try running it:

jupyter-run-js

Using Jupyter Notebook in VSC

First, make sure the Jupyter plugin is installed.

Second, open Jupyter Notebook in VSC:

jupyter-in-vsc

To successfully run the environment, you must configure Jupyter Server, otherwise the default environment cannot run JavaScript runtime. The main reason I use VSC is that I feel that the prompt function of VSC is slightly better than that of the native Jupyter Notebook. Of course, it may also be because I rarely use Jupyter Notebook and am not very familiar with the shortcut keys.

Using Node Modules in Jupyter Notebook

Simply use npm init to initialize the root directory as a node project, and then import it normally:

node-modules

Note that if you are unsure about the variable name you want to reference, it is better to use var declaration, because this environment is global. If you use const declaration for experimental purposes, you may get an error (after all, it cannot be modified).

Also, it is best to declare them separately to avoid errors because the variables have already been declared:

declared-var

This is the end of this article about how to run JavaScript in Jupyter Notebook. For more information about running JavaScript in Jupyter Notebook, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of JavaScript operation mechanism and a brief discussion on Event Loop
  • Learn the operating mechanism of jsBridge in one article
  • Let you understand the working principle of JavaScript
  • Detailed process of installing Docker, creating images, loading and running NodeJS programs
  • Solve the problem that running js files in the node terminal does not support ES6 syntax
  • Tutorial on compiling and running HTML, CSS and JS files in Visual Studio Code
  • Example of running JavaScript with Golang
  • Front-end JavaScript operation principle

<<:  Detailed explanation of the solution to the error of using systemctl to start the service in docker

>>:  Example of how to quickly delete a 2T table in mysql in Innodb

Recommend

How to use linux commands to convert and splice audio formats

Install FFmpeg flac eric@ray:~$ sudo apt install ...

Webpack builds scaffolding to package TypeScript code

Create a folder Directory structure: dabaots Init...

Embed player in web page embed element autostart false invalid

Recently, I encountered the need to embed a player...

MySQL database development specifications [recommended]

Recently, we have been capturing SQL online for o...

How to use the jquery editor plugin tinyMCE

Modify the simplified file size and download the ...

How to solve the mysql ERROR 1045 (28000)-- Access denied for user problem

Problem description (the following discussion is ...

Detailed explanation of JavaScript primitive data type Symbol

Table of contents Introduction Description Naming...

JS practical object-oriented snake game example

Table of contents think 1. Greedy Snake Effect Pi...

Four ways to switch tab pages in VUE

Table of contents 1. Static implementation method...

Node uses async_hooks module for request tracking

The async_hooks module is an experimental API off...