Skip to main content

WolframAlpha Python API Wrapper

This is an API wrapper for the Wolfram Alpha API. It contains functions for the Simple, Short Answers, Full Results and Conversational APIs.

Installation

Python 3.5 of higher is required

Linux & macOS

python3 -m pip install wolframalpha.py

Windows

py -3 -m pip install wolframalpha.py

You can always use the Github Dev version before the release on Pypi by cloning the repo and upgrading the package locally.

Getting Started

  • You must have a WolframAlpha account in order to get the AppID. You can create an account here.
  • Then you must create an application at the WolframAlpha Developer Portal. AppID
  • Copy the AppID. You will use this to initialize the WolframPy Application later on.

Application Example

from wolfram import App

wolfram = App("APP_ID") # Make sure to replace the ID with your own


"""
This will generate an image with information about the american population and save it with the name "america_population.png". If no second param is provided, it will be saved as "wolframpy_content.png"
"""
wolfram.simple("Population of America", "america_population")

Documentation

class wolfram.App(appID)

class App:
    def __init__(self, appID):
        self.id = appID
        self.SIMPLE_BASE = "https://api.wolframalpha.com/v1/simple"
        self.FULL_BASE = "https://api.wolframalpha.com/v2/query"
        self.SHORT_BASE = "http://api.wolframalpha.com/v1/result"
        self.CONV_BASE = "http://api.wolframalpha.com/v1/conversation.jsp"

    """
    Create a request to the WolframAPI
    """
    def create_request(self, BASE, **kwargs):
        return requests.get(BASE + "?" + '&'.join(f"{i}={kwargs[i]}" for i in kwargs) + "&appid=" + self.id)

    """
    Fetch an info image and save it locally

    Example: wolfram.App(app_id).simple("Value of Gold", "valueOfGold")
    """
    def simple(self, query:lambda arg:fix_format(arg), fp:lambda arg:str(arg)="wolframpy_content"):
        _data = self.create_request(self.SIMPLE_BASE, i=query)

        if _data.status_code == 200:
            with open(fp+".png", "wb") as f:
                for chunk in _data.iter_content(1024):
                    f.write(chunk)
        
        if _data.status_code == 404:
            raise APIError("The WolframAPI is currently unreachable!")
        
        if _data.status_code == 501:
            raise InputError("Could not understand input.")

    """
    Receive a dictionary of information about the query. Recommended for experts.

    Example: wolfram.App(app_id).full("Value of Gold")
    """
    def full(self, query:lambda arg:fix_format(arg)):
        return self.create_request(self.FULL_BASE, input=query, output="json").json()

    """
    Receive a line-long answer to a query
    """
    def short(self, query:lambda arg:fix_format(arg)):
        return self.create_request(self.SHORT_BASE, i=query).text

    """
    Use the ConversationalAPI to talk. Returns a dictionary

    Example: wolfram.App(app_id).talk("How are you?")
    """
    def talk(self, query:lambda arg:fix_format(arg)):
        return self.create_request(self.CONV_BASE, i=query, s=5).json()

Asynchronous Usage

Example

import wolfram, asyncio, json

app = wolfram.AsyncApp("APP_ID")

async def getQuery():
    data = json.loads(await app.full("Population of America"))
    print(data)
    with open("wolfram.json", "w+") as f:
        json.dump(data, f, indent=4)

loop = asyncio.get_event_loop()

try:
    loop.run_until_complete(getQuery())
finally:
    loop.close()

Metadata

Release files for WolframAlpha.py 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for WolframAlpha.py 1.2.0
File Size Uploaded
WolframAlpha.py-1.2.0.tar.gz 4.1 kB Details

Release files / WolframAlpha.py-1.2.0.tar.gz

Download URL WolframAlpha.py-1.2.0.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
51d5cd72ed5b77ffa60ab67f559f92160fc1258def915aa604d657497d14ce8e
BLAKE2b-256 checksum
How to use checksums
db4a004955867e695f4069556c82905c384091c1e388b59ab1f7b7e82c15807b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

Release history Release notifications | RSS feed

This release

1.2.0 This release

1 release file

1.1.9

1 release file

1.1.8

1 release file

1.1.7

1 release file

1.1.6

1 release file

1.1.5

1 release file

1.1.4

1 release file

1.1.3

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page