Welcome to wolframalpha documentation!¶
- class wolframalpha.Client(app_id, **kwargs)¶
Bases:
objectWolfram|Alpha v2.0 client
Basic usage is pretty simple. Get your App ID at https://products.wolframalpha.com/api/. Create the client with your App ID:
>>> app_id = getfixture('API_key') >>> client = Client(app_id)
Send a query, which returns Results objects:
>>> res = client.query( ... 'temperature in Washington, DC on October 3, 2012', ... location='earth', ... units='metric', ... )
Wolfram|Alpha tailors results (units, how places are named) to the caller’s location, inferred from their IP address unless supplied. Passing
location(orlatlongorip) andunitsmakes results independent of where the query originates.Result objects have pods (a Pod is an answer group from Wolfram Alpha):
>>> for pod in res.pods: ... pass # do_something_with(pod)
Pod objects have
subpods(a Subpod is a specific response with the plaintext reply and some additional info):>>> for pod in res.pods: ... for sub in pod.subpods: ... print(sub.plaintext) temperature | Washington, United States Wednesday, October 3, 2012 (21 to 27) °C (average: 24 °C) ...
To query simply for the pods that have ‘Result’ titles or are marked as ‘primary’ using
Result.results:>>> print(next(res.results).text) (21 to 27) °C (average: 24 °C) (Wednesday, October 3, 2012)
All objects returned are dictionary subclasses, so to find out which attributes Wolfram|Alpha has supplied, simply invoke
.keys()on the object. Attributes formed from XML attributes can be accessed with or without their “@” prefix (added by xmltodict).- async aquery(input, params=(), **kwargs)¶
- classmethod from_env()¶
Create a client with a key discovered from the keyring or environment variable. Raises an exception if no key is found.
- query(input, params=(), **kwargs)¶
Query Wolfram|Alpha using the v2.0 API
Allows for arbitrary parameters to be passed in the query. For example, to pass assumptions:
>>> client = Client(getfixture('API_key')) >>> res = client.query(input='pi', assumption='*C.pi-_*NamedConstant-')
To pass multiple assumptions, pass multiple items as params:
>>> params = ( ... ('assumption', '*C.pi-_*NamedConstant-'), ... ('assumption', 'DateOrder_**Day.Month.Year--'), ... ) >>> res = client.query(input='pi', params=params)
For more details on Assumptions, see https://products.wolframalpha.com/api/documentation.html#6
- timeout: float = 30.0¶
Seconds to wait for the API to respond. Wolfram|Alpha’s own
totaltimeoutdefaults to about 20 seconds, so allow somewhat more than that (rather than httpx’s default of 5 seconds). Passtimeout=to the constructor to override.
- url = 'https://api.wolframalpha.com/v2/query'¶
- class wolframalpha.Image¶
Bases:
DocumentHolds information about an image included with an answer.
- key = 'img'¶
- class wolframalpha.Pod(*args, **kwargs)¶
Bases:
ErrorHandler,DocumentGroups answers and information contextualizing those answers.
- property primary¶
- property text¶
- property texts¶
The text from each subpod in this pod as a list.
- class wolframalpha.Result(*args, **kwargs)¶
Bases:
ErrorHandler,DocumentHandles processing the response for the programmer.
- property details¶
A simplified set of answer text by title.
- property info¶
The pods, assumptions, and warnings of this result.
- key = 'queryresult'¶
- property results¶
The pods that hold the response to a simple, discrete query.
- class wolframalpha.Subpod¶
Bases:
DocumentHolds a specific answer or additional information relevant to said answer.
- wolframalpha.identity(x)¶
- wolframalpha.xml_bool(str_val)¶
>>> xml_bool('true') True >>> xml_bool('false') False