How to use the Wolfram Language in a Jupyter Notebook¶

First, you need to have a licence of Mathematica (https://www.wolfram.com/mathematica/pricing/students/)

Then, install the Wolfram Client Library (https://reference.wolfram.com/language/WolframClientForPython/index.html)

In [1]:
#pip install wolframclient
In [2]:
#pip install wolframalpha

Ensure that the librairies with pip are installed and restart the kernel. Now, you can start your session with the Wolfram Language (WL). You may need to specify the folder for the Kernel.

In [3]:
from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wl, wlexpr
session = WolframLanguageSession('C:\\Program Files\\Wolfram Research\\Mathematica\\13.2\\WolframKernel.exe')

Evalutate using Mathematica functions, for example MinMax (https://reference.wolfram.com/language/ref/MinMax.html)

In [4]:
session.evaluate(wl.MinMax([1, 5, -3, 9]))
Out[4]:
(-3, 9)

Acces the Wolfram Database to retrieve the population density of the world with 'wl'

In [5]:
session.evaluate(wl.WolframAlpha("Population density of the world", "Result"))
Out[5]:
Quantity[59.29148808856, Times[Power['Kilometers', -2], 'People']]

Create and insert your Wolfram Alpha API ID and ask a query

In [6]:
import wolframalpha

# Replace 'YOUR_APP_ID' with your actual Wolfram Alpha API App ID
app_id = '8...X'
client = wolframalpha.Client(app_id)
In [7]:
query = input("Enter your query: ")  # Ask the user for a query
res = client.query(query)

# Print and display the results
for pod in res.pods:
    print(pod.title)
    print('-' * len(pod.title))
    print(pod.text)
    print()
Enter your query: Population density of the world
Input interpretation
--------------------
world | population density

Result
------
59.3 people per square kilometer

Population
----------
population | 7.79 billion people (2020 estimate)
population density | 59.3 people/km^2 (people per square kilometer)

Unit conversions
----------------
5.93×10^-5 people per square meter

Corresponding quantity
----------------------
Area per person:
 | 182000 square feet per person

Evaluate a limit with 'wlexpr'

In [8]:
limit = wlexpr('Limit[x Log[x^2], x -> 0]')
session.evaluate(limit)
Out[8]:
0
In [9]:
query = input("Enter your query: ")  # Ask the user for a query
res = client.query(query)

# Print and display the results
for pod in res.pods:
    print(pod.title)
    print('-' * len(pod.title))
    print(pod.text)
    print()
Enter your query: Limit of x log(x^2) when x goes to 0
Limit
-----
lim_(x->0) x log(x^2) = 0

Expanded logarithmic form
-------------------------
lim_(x->0) x 2 log(x)

Plot
----
None

Series expansion at x = 0
-------------------------
x log(x^2)

Retrieve information on a historical figure

In [10]:
session.evaluate(wl.WolframAlpha("J. Robert Oppenheimer", "Result"))
Out[10]:
Entity['Person', 'JRobertOppenheimer::g6wj5']
In [11]:
query = input("Enter your query: ")  # Ask the user for a query
res = client.query(query)

# Print and display the results
for pod in res.pods:
    print(pod.title)
    print('-' * len(pod.title))
    print(pod.text)
    print()
Enter your query: Who was Robert Oppenheimer?
Input interpretation
--------------------
J. Robert Oppenheimer (physicist)

Basic information
-----------------
full name | Julius Robert Oppenheimer
date of birth | Friday, April 22, 1904 (119 years ago)
place of birth | New York City, New York, United States
date of death | Saturday, February 18, 1967 (age: 62 years) (56 years ago)
place of death | Princeton, New Jersey, United States

Image
-----
None

Timeline
--------
None

Notable facts
-------------
Theoretical physicist regarded as the father of the atomic bomb for his part in the Manhattan Project during World War II
Promoted international control of nuclear power, in order to prevent nuclear proliferation and an arms race
Became a chief adviser for the new United States Atomic Energy Commission after World War II
Saw his security clearance revoked during the Second Red Scare in response to his outspoken views
Taught as a professor of physics and is a key figure in the American school of theoretical physics that rose to note in the 1930s
Noted for the Born--Oppenheimer approximation for molecular wave functions, and the Oppenheimer--Phillips process in nuclear fusion

Physical characteristics
------------------------
height | 1.78 meters

Familial relationships
----------------------
Julius S. Oppenheimer | Ella Friedman

Scientific contributions
------------------------
Born-Oppenheimer approximation

Notable films
-------------
Trinity and Beyond: The Atomic Bomb Movie (1995) | Countdown to Zero (2010) | The Day After Trinity (1981) | Atomic Power (1946)

Wikipedia summary
-----------------
J. Robert Oppenheimer (April 22, 1904 - February 18, 1967) was an American theoretical physicist. He was the director of the Los Alamos Laboratory during World War II, and is often credited as the "father of the atomic bomb" for his role in the Manhattan Project, the research and development undertaking that created the first nuclear weapons.

Wikipedia page hits history
---------------------------
None

Finally, terminate the session

In [12]:
session.terminate()