Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, render_template, Markup
- import pandas as pd
- from bokeh.plotting import figure, output_file, show
- from bokeh.embed import components
- from bokeh.resources import CDN
- from bokeh.layouts import column,layout, widgetbox
- from bokeh.models.widgets import Select
- from bokeh.models import ColumnDataSource
- from bokeh.io import curdoc
- df=pd.read_csv('allgames.csv')
- #get a unique list of teams using set function
- teams=set(df['awayteam'].tolist())
- teamslist=list(teams)
- teamgen=iter(teams)
- myteam=next(teamgen)
- #takes a team and makes their dataframe
- def makedf(myteam):
- df1=df[df['awayteam']==myteam]
- df2=df[df['hometeam']==myteam]
- df3=pd.concat([df1,df2])
- df3=df3.sort_values('date')
- return df3
- teamdf=makedf(myteam)
- #make into a generator so you can pass it into the function and generate a graph one by one
- #next step is to create a dataframe that is sorted by date for the games that your team played.
- #do this by iterating over the dataframe with the iterrows() function.. however since it returns a tuple you must unpack
- # create two empty lists, one for the opening line and one for the closing line
- def makeplot(teamdf,myteam):
- lineopen=[]
- lineclose=[]
- for k,v in teamdf.iterrows():
- if v.awayteam==myteam:
- lineopen.append(v.awayopener)
- lineclose.append(v.awaypinny)
- else:
- lineopen.append(v.homeopener)
- lineclose.append(v.homepinny)
- p=figure(plot_width=700,plot_height=700,title=myteam)
- p.background_fill_color="red"
- p.background_fill_alpha=0.8
- p.background_fill_alpha=0.4
- p.title.text_font_style="bold"
- p.outline_line_color="navy"
- p.outline_line_width=5
- p.outline_line_alpha=0.3
- x=[i for i in range(1,83)]
- p.circle(x,lineopen,fill_color="green",size=10,fill_alpha=0.8,name="openinglines",legend=myteam+" Opening Lines")
- ####################################
- p.circle(x,lineclose,fill_color="red",size=10,fill_alpha=0.8,name="closinglines",legend=myteam+" Pinnacle Closing Line")
- return p
- graph=makeplot(teamdf,myteam)
- ts1=column(graph)
- select=Select(value=myteam, options=teamslist)
- widget=widgetbox(select)
- layout=column(widget,ts1)
- def ticker1_change(attrname, old, new):
- global layout, ts1, newteam, picked, graph
- newteam=select.value
- picked=makedf(newteam)
- graph=makeplot(picked,newteam)
- ts1=column(graph)
- layout=column(widget,ts1)
- curdoc().add_root(layout)
- select.on_change('value',ticker1_change)
- curdoc().add_root(layout)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement