Created
April 28, 2015 02:59
-
-
Save gparuthi/22c7d1f6761231e2c90c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import datetime | |
from dateutil import parser | |
import pandas as pd | |
import matplotlib as mpl | |
mpl.use('Agg') | |
import matplotlib.pyplot as plt | |
# plt.style.use('ggplot') | |
from bs4 import BeautifulSoup | |
import StringIO | |
import base64 | |
import json | |
import warnings | |
warnings.filterwarnings("ignore") | |
def get_plot_as_b64_png(ax): | |
fig = ax.figure | |
imgdata = StringIO.StringIO() | |
fig.savefig(imgdata, format='png') | |
imgdata.seek(0) # rewind the data | |
fin_dta = base64.b64encode(imgdata.buf) | |
return fin_dta | |
def apply(input): | |
global df | |
city = input.get('city','arb').upper() | |
try: | |
url = "http://w1.weather.gov/obhistory/K%s.html"%city | |
r = requests.get(url) | |
html_data = r.text | |
rows = [row for row in BeautifulSoup(html_data)("tr")][::-1] | |
plist = [] | |
for row in rows: | |
cols = row('td') | |
if len(cols) > 4: | |
pressure = cols[13].text | |
time = cols[1].text | |
dt = parser.parse(time) | |
plist.append({'time': dt, 'pressure':float(pressure)}) | |
df = pd.DataFrame(plist) | |
plt.xlabel('Hourly reading') | |
plt.ylabel('Air Pressure') | |
plt.title('Air Pressure in %s'%city) | |
ax = df.pressure.plot() | |
fin_dta = get_plot_as_b64_png(ax) | |
res = {'png':fin_dta, 'city': city} | |
return res | |
except Exception as e: | |
print "Error ",e | |
return "error" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment