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

Control the vertical center of the text in the HTML text box through CSS

When the height attribute of Text is defined, the ...

Nginx defines domain name access method

I'm building Nginx recently, but I can't ...

Summary of the use of TypeScript in React projects

Preface This article will focus on the use of Typ...

Solution to mysql login warning problem

1. Introduction When we log in to MySQL, we often...

Summary of Mysql slow query operations

Mysql slow query explanation The MySQL slow query...

Use Xshell to connect to the Linux virtual machine on VMware (graphic steps)

Preface: I recently started to study the construc...

File upload via HTML5 on mobile

Most of the time, plug-ins are used to upload fil...

How to use nginx to block a specified interface (URL)

1. Introduction Sometimes, after the web platform...

What scenarios are not suitable for JS arrow functions?

Table of contents Overview Defining methods on an...

How to use js to communicate between two html windows

Scenario: When page A opens page B, after operati...

How to simulate enumeration with JS

Preface In current JavaScript, there is no concep...

Summary of basic usage of js array

Preface Arrays are a special kind of object. Ther...

JavaScript imitates Jingdong magnifying glass effect

This article shares the specific code for JavaScr...

Vue implements infinite loading waterfall flow

This article example shares the specific code of ...