Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example PyData app on Cloud Foundry using Spyre (https://github.com/adamhajari/spyre) and the Python conda buildpack (https://github.com/ihuston/python-conda-buildpack)
numpy
scipy
pandas
matplotlib
---
applications:
- name: ih-spyre-test
memory: 1GB
buildpack: git://github.com/ihuston/python-conda-buildpack.git
timeout: 180
command: .conda/bin/python stocks.py
cherrypy
dataspyre==0.1.0
"""Example Spyre app from https://github.com/adamhajari/spyre"""
import matplotlib
# Needed to use headless
matplotlib.use('Agg')
from spyre import server
import os
import matplotlib.pyplot as plt
import numpy as np
class SimpleSineApp(server.App):
title = "Simple Sine App"
inputs = [{ "input_type":"text",
"variable_name":"freq",
"value":5,
"action_id":"sine_wave_plot"}]
outputs = [{"output_type":"plot",
"output_id":"sine_wave_plot",
"on_page_load":True }]
def getPlot(self, params):
f = float(params['freq'])
print f
x = np.arange(0,2*np.pi,np.pi/150)
y = np.sin(f*x)
fig = plt.figure()
splt1 = fig.add_subplot(1,1,1)
splt1.plot(x,y)
return fig
port = int(os.getenv("VCAP_APP_PORT"))
app = SimpleSineApp()
app.launch(port=port, host='0.0.0.0')
## PyData stack on Cloud Foundry
The current Python buildpack uses `pip` to install dependencies. Anyone who has tried to install NumPy or SciPy using `pip` knows that the process can be lengthy and painful often requiring manual intervention to correct library paths and install Fortran compilers.
Fortunately [Continuum Analytics'](http://continuum.io/) [conda package manager](conda.pydata.org) was created to solve these problems by packaging and distributing the standard tools of the Python data stack in compiled binaries.
I wanted to build web apps on Cloud Foundry using the PyData stack so with help from [a colleague](https://github.com/nebhale) I have written a [Cloud Foundry buildpack](https://github.com/ihuston/python-conda-buildpack) which uses both conda and pip to install required packages.
You can specify packages to be installed by `conda` in the `conda_requirements.txt` file and these will be installed first, followed by packages in the `requirements.txt` which will be installed as usual by `pip`.
This web app uses [Spyre](https://github.com/adamhajari/spyre) by Adam Hajari which is available on PyPI as `dataspyre`.
"""Example Spyre app from https://github.com/adamhajari/spyre"""
import matplotlib
# Needed to use headless
matplotlib.use('Agg')
import os
from spyre import server
import pandas as pd
import urllib2
import json
class StockExample(server.App):
title = "Historical Stock Prices"
inputs = [{ "input_type":'dropdown',
"label": 'Company',
"options" : [ {"label": "Google", "value":"GOOG"},
{"label": "Yahoo", "value":"YHOO"},
{"label": "Apple", "value":"AAPL"}],
"variable_name": 'ticker',
"action_id": "update_data" }]
controls = [{ "control_type" : "hidden",
"label" : "get historical stock prices",
"control_id" : "update_data"}]
tabs = ["Plot", "Table"]
outputs = [{ "output_type" : "plot",
"output_id" : "plot",
"control_id" : "update_data",
"tab" : "Plot",
"on_page_load" : True },
{ "output_type" : "table",
"output_id" : "table_id",
"control_id" : "update_data",
"tab" : "Table",
"on_page_load" : True }]
def getData(self, params):
ticker = params['ticker']
# make call to yahoo finance api to get historical stock data
api_url = 'https://chartapi.finance.yahoo.com/instrument/1.0/{}/chartdata;type=quote;range=3m/json'.format(ticker)
result = urllib2.urlopen(api_url).read()
data = json.loads(result.replace('finance_charts_json_callback( ','')[:-1]) # strip away the javascript and load json
self.company_name = data['meta']['Company-Name']
df = pd.DataFrame.from_records(data['series'])
df['Date'] = pd.to_datetime(df['Date'],format='%Y%m%d')
return df
def getPlot(self, params):
df = self.getData(params)
plt_obj = df.set_index('Date').drop(['volume'],axis=1).plot()
plt_obj.set_ylabel("Price")
plt_obj.set_title(self.company_name)
fig = plt_obj.get_figure()
return fig
port = int(os.getenv("VCAP_APP_PORT"))
app = StockExample()
app.launch(port=port, host='0.0.0.0')
@ebergel

This comment has been minimized.

Copy link

ebergel commented Dec 20, 2014

Hi, I tried to reproduce what you did, but there might be a problem to install matplotlib
I got this error when I push the app
ERR grep: ./app/.conda/pkgs/matplotlib-1.4.2-np19py27_0/lib/libtcl.so: No such file or directory
any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment