A WolframAlpha API Wrapper for Python.
Project description
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.
- 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()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
WolframAlpha.py-1.2.0.tar.gz
(4.1 kB
view details)
File details
Details for the file WolframAlpha.py-1.2.0.tar.gz.
File metadata
- Download URL: WolframAlpha.py-1.2.0.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
51d5cd72ed5b77ffa60ab67f559f92160fc1258def915aa604d657497d14ce8e
|
|
| MD5 |
146111bf47b9ad06a1dd3363fa9de2c0
|
|
| BLAKE2b-256 |
db4a004955867e695f4069556c82905c384091c1e388b59ab1f7b7e82c15807b
|