Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example PyData app on Cloud Foundry using Spyre ( and the Python conda buildpack (
- name: ih-spyre-test
memory: 1GB
buildpack: git://
timeout: 180
command: .conda/bin/python
"""Example Spyre app from"""
import matplotlib
# Needed to use headless
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",
outputs = [{"output_type":"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)
return fig
port = int(os.getenv("VCAP_APP_PORT"))
app = SimpleSineApp()
app.launch(port=port, host='')
## 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']( [conda package manager]( 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]( I have written a [Cloud Foundry 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]( by Adam Hajari which is available on PyPI as `dataspyre`.
"""Example Spyre app from"""
import matplotlib
# Needed to use headless
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 = '{}/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()
fig = plt_obj.get_figure()
return fig
port = int(os.getenv("VCAP_APP_PORT"))
app = StockExample()
app.launch(port=port, host='')

This comment has been minimized.

Copy link

@ebergel 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/ 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