So today we are happy to formally announce a new and often requested connection, which allows you to call the Wolfram Language directly and efficiently from Python: The Wolfram Client Library for Python! And, even better, this client library is fully open sourced as the WolframClientForPython git repository under the MIT License so you can clone it and use it any way you see fit! And if you see a way to improve it, please help us make it better by contributing pull requests to this repository.
“The Wolfram Client Library for Python is full open sourced.”
It’s Easy! The client library will make it super easy to integrate the large collection of Wolfram Language algorithms and knowledge directly into any Python code that you already have and will save you time and effort when developing new code. In this post we will show how to set up a connection from Python to the Wolfram Language and a few methods and examples you can use to do a computation in the Wolfram Language and use it in your Python session. For a complete introductory tutorial and full reference documentation you can visit the documentation homepage for the Wolfram Client Library for Python.
Evaluate Locally… We will start with a simple example which computes the mean and standard deviation of one million numbers drawn from a normal distribution. This example shows how to call a Wolfram Language function from Python and we compare the results from Python and the Wolfram Language to show that they a numerically close to one another.
Statistical Data Analysis
First, to connect to the Wolfram Language we need to create a new session with the Wolfram Engine:
〉
>>> from wolframclient.evaluation import WolframLanguageSession >>> session = WolframLanguageSession()
To call Wolfram Language functions we need to import the `wl` factory:
〉
>>> from wolframclient.language import wl
Now we can evaluate any Wolfram Language code. Here we set the Python variable sample to a list of 1,000,000 random numbers drawn from the normal distribution with a mean of 0 and a standard deviation of 1:
And run the following code in Python to verify that we get a similar result:
〉
>>> from statistics import stdev >>> stdev(sample) 1.0014296230797068
It's good to see that these results are comparable, so now we know how to call some simple Wolfram Language functions from Python. Let’s continue with a more exciting example!
Using the Wolfram Knowledgebase
Now let's take a look at a built-in Wolfram Language function that is not readily available in Python:
〉
moons = session.evaluate( wl.WolframAlpha('moons of Saturn', 'Result') )
The output here is the Python representation of a Wolfram Language expression which can be re-used in any subsequent evaluation. For example, if we want to get the list of (the first four) moons explicitly we can do this:
This is all pretty cool! Let take a look at another example, using the Wolfram Language’s built-in image processing and machine learning functions.
Image Processing & Machine Learning
First let’s switch over to another mode to do evaluations directly in the Wolfram Language. So far we have used the wl factory to build up Wolfram Language expressions in Python. But you can also evaluate Python strings containing Wolfram Language code and sometimes this is easier to read:
〉
>>> from wolframclient.language import wlexpr
For example, we can solve the quadratic equation by sending the Wolfram Language code as a string:
〉
>>> session.evaluate('1+1') 2
Using this method we can write a small snippet of Wolfram Language code which gets an image and uses the built-in face detection algorithm to find the location of the face in this image. Here the image we are using is the famous painting titled “Girl with a Pearl Earring” by the Dutch painter Johannes Vermeer (but it works on almost any image with recognizable faces!). Since the Python terminal interface does not support the display of image, we use a Jupyter notebook instead, together with the Python Image Library (PIL) package to help with displaying the result:
Quite easy and powerful! But what if you don’t have a local installation of the Wolfram Engine and want to use the Wolfram Client Library for Python? You can still use the Wolfram Language directly in the Wolfram Cloud.
To the Cloud! The Wolfram Cloud provides easy access to the Wolfram Language without needing to install it locally. The Wolfram Cloud provides various services, including a notebook web interface for Wolfram Language programming as well as the capability to deploy arbitrary Wolfram Language web APIs.
Here we will make use of the latter, deploying a Wolfram Language web API. This particular accepts the names of two countries (country1 and country2), find the capital city for each country and then computes the distance between them (in kilometers):
After this deployment of this API we can start a new Wolfram Language session, but this time we connect to the Wolfram Cloud instead:
〉
>>> from wolframclient.evaluation WolframCloudSession >>> cloud = WolframCloudSession()
To call the API we have to provide the username (user1) and the API endpoint (api/public/capital_distance). With that information, we can call the cloud:
〉
>>> api = ('user1', 'api/public/capital_distance') >>> result = cloud.call(api, {'country1': 'Netherlands', 'country2': 'Spain'})
And get the result:
〉
>>> result.get() 1481.4538329484521
Pretty easy and useful!
If you want to keep your deploy Wolfram Language API private, so that only you can use it, you can deploy the API with Permissions->”Private”. Then, to authenticate yourself to the private API, you can generate (in the Wolfram Language) a secured authentication key:
We are very excited about this release and hope you find it useful. Please let us know what you think by posting feedback on this blog or by making a post on the Wolfram Community.
Cite this as: Dorian Birraux, Todd Gayley, Arnoud Buzing, "Announcing the Wolfram Client Library for Python" from the Notebook Archive (2019), https://notebookarchive.org/2019-10-3ppx2n1