# Extend your python with Online Mathematica API

**URL:** <https://community.wolfram.com/t/extend-your-python-with-online-mathematica-api/2409>\
**Category:** Ideas\
**Tags:** computer-science, mathematica, external-interfaces\
**Created:** [June 19, 2014, 8:32pm UTC](https://community.wolfram.com/t/extend-your-python-with-online-mathematica-api/2409 "2014-06-19T20:32:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shenghuiy](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/shenghuiy/32/36073_2.png) [@shenghuiy](https://community.wolfram.com/u/shenghuiy)\
**Post date:** [June 19, 2014, 8:32pm UTC](https://community.wolfram.com/t/extend-your-python-with-online-mathematica-api/2409/1 "2014-06-19T20:32:35Z")

</div>

**Update:** Do not miss [this online event for API](http://www.wolfram.com/training/special-event/wolfram-language-in-the-cloud-2014/)

This post is to show how to enhance your python with Mathematica online api with an elaborated example. You may refer to [this video](https://www.youtube.com/watch?v=BCHsvowZuaE) for basic ideas before you try the steps below.

 ![ScreenShot2014-06-19at2.15.24PM.png](https://global.discourse-cdn.com/wolfram/original/3X/a/b/abf2865f6003dea07327c6dfd59cac6c85633560.png)

Let’s take a look at the product. I have defined one function in python, `apiPlot`. This function calls a customized API from Mathematica Online deployment and render icosahedron ball. Python opens it locally as the output. You can modify the parameters as well to get several different plots.

 ![4300result.jpg](https://global.discourse-cdn.com/wolfram/original/3X/c/c/cca198db6ab5c0fd6a498388b2ac4d31c64c8151.jpeg)

You need the following steps to implement the code

- Get a Mathematica online account for development if you do not have

- Use the modified code from [this example](http://demonstrations.wolfram.com/IcosahedronBall/) (available in the attached notebook) and implement it in the online notebook

 ![ScreenShot2014-06-19at12.59.27PM.png](https://global.discourse-cdn.com/wolfram/original/2X/9/91ebfcc3c534746b8811e7377eb33bbdbbba6646.png)

- Evaluate the online notebook and make sure it is evaluated correctly

- Click the Deploy button on the top-right corner can choose **Instant API**

 ![ScreenShot2014-06-19at12.59.41PM.png](https://global.discourse-cdn.com/wolfram/original/3X/5/1/51a9999566a0ead6e81e8baef22fd9e956d9cf2e.png)

- Click **Add** to pick up the function that you want to call from the API. In my case, just add “f”

 ![ScreenShot2014-06-19at1.00.00PM.png](https://global.discourse-cdn.com/wolfram/original/3X/c/2/c2d410edd66cba685acbaddd9df7169e4b82d2bb.png)

- Scroll down to expand the “f” and check its argument list (click the circled opener to reveal the details). You can click **test api** to run the function directly from within the window after you put the values into the function.

 ![publish.png](https://global.discourse-cdn.com/wolfram/original/2X/c/cb8538f05edfcec976b99007eeba864c9ccbf3a4.png)

In this case there is only one function involved and the result in the notebooks is correct, you can just go ahead and click the **publish**.

On the Python side, the implementation is rather simple with [default argument support](https://docs.python.org/release/1.5.1p1/tut/defaultArgs.html) (you will need PIL package for Image):

```
import urllib
import Image 
def apiPlot(conv=6,dis=-3,icos=-1,sep=0.2):
   head= "http://online.wolfram.com/api/shenghui/demo/f?"
   inputList={"conv":conv,"dis":dis,"icos":icos,"sep":sep}
   encodedlist = urllib.urlencode(inputList)
   urllib.urlretrieve(head+encodedlist,"test.jpg")
   img = Image.open("test.jpg")
   img.show()

```

Once you save it in the `myApi.py` file and run the code in terminal:

```
$: python -i myApi.py
 >>> apiPlot()

```

you can get the same result as my very first screenshot. In fact this piece of code is quite universal, you can change api function/argument names and call most plot/graphics functions via Mathematica online API.

You can refer to more detailed Python+Wolfram Alpha API tutorial from the following link

- [Python +Wolfram Alpha API](https://wolfram.discourse.group/t/1847/5)

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/szhorvat/32/3531_2.png) [@szhorvat](https://community.wolfram.com/u/szhorvat)\
**Post date:** [June 19, 2014, 8:40pm UTC](https://community.wolfram.com/t/extend-your-python-with-online-mathematica-api/2409/2 "2014-06-19T20:40:03Z")

</div>

Accessing this online API does indeed work well: [http://online.wolfram.com/api/shenghui/demo/f?conv=6&dis=-3&icos=-1&sep=0.2](http://online.wolfram.com/api/shenghui/demo/f?conv=6&dis=-3&icos=-1&sep=0.2)

> - Get a Mathematica online account for development if you do not have

Since this short tutorial was posted today, when will this be possible for the public?

---

<div class="post-metadata">

**Author:** ![shenghuiy](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/shenghuiy/32/36073_2.png) [@shenghuiy](https://community.wolfram.com/u/shenghuiy)\
**Post date:** [June 19, 2014, 9:12pm UTC](https://community.wolfram.com/t/extend-your-python-with-online-mathematica-api/2409/3 "2014-06-19T21:12:56Z")

</div>

I am not sure about the exact release date. But you can contact [support@wolfram.com](mailto:support@wolfram.com) to request for a developer account if possible.
