Skip to content

Instantly share code, notes, and snippets.

@drdrang
Last active August 29, 2015 14:03
Show Gist options
  • Save drdrang/4adb7445f9f90b387f5b to your computer and use it in GitHub Desktop.
Save drdrang/4adb7445f9f90b387f5b to your computer and use it in GitHub Desktop.
Evaluate an expression with a numerical answer using Wolfram Alpha and return the result as an equation. Also open the Alpha page in a browser (Mac only). Intended to be used as a BBEdit Text Filter but can be used from the command line. See http://www.leancrew.com/all-this/2014/07/evaluating-latex-with-eddie-and-alpha/
#!/usr/bin/python
from urllib import quote_plus
from sys import stdin
from subprocess import call
import requests
import xml.etree.ElementTree as ET
appID = 'XXXXXX-YYYYYYYYYY' # Placeholder. You'll need to get an ID from http://products.wolframalpha.com/api/
def get_plaintext_query(latex):
r = requests.get('http://api.wolframalpha.com/v2/query?input=%s&appid=%s' % (quote_plus(latex), appID))
root = ET.fromstring(r.text.encode('utf8'))
for pod in root:
if pod.attrib.get('title', '') in ['Decimal approximation', 'Definite integral', 'Decimal form', 'Result']:
subpod = pod.find('subpod')
result = subpod.find('plaintext').text
if pod.attrib.get('title', '') == 'Definite integral':
return result.split('~~')[1]
else:
return result
if __name__ == '__main__':
latex = stdin.read()
alphaURL = "http://www.wolframalpha.com/input/?i=%s" % quote_plus(latex)
print latex + ' = ' + get_plaintext_query(latex)
call(['open', alphaURL])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment