Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. from flask import Flask, render_template, Markup
  2. import pandas as pd
  3. from bokeh.plotting import figure, output_file, show
  4. from bokeh.embed import components
  5. from bokeh.resources import CDN
  6. from bokeh.layouts import column,layout, widgetbox
  7. from bokeh.models.widgets import Select
  8. from bokeh.models import ColumnDataSource
  9. from bokeh.io import curdoc
  10.  
  11.  
  12. df=pd.read_csv('allgames.csv')
  13. #get a unique list of teams using set function
  14. teams=set(df['awayteam'].tolist())
  15. teamslist=list(teams)
  16. teamgen=iter(teams)
  17. myteam=next(teamgen)
  18.  
  19. #takes a team and makes their dataframe
  20. def makedf(myteam):
  21.     df1=df[df['awayteam']==myteam]
  22.     df2=df[df['hometeam']==myteam]
  23.     df3=pd.concat([df1,df2])
  24.     df3=df3.sort_values('date')
  25.     return df3
  26.  
  27. teamdf=makedf(myteam)
  28. #make into a generator so you can pass it into the function and generate a graph one by one
  29.  
  30. #next step is to create a dataframe that is sorted by date for the games that your team played.
  31. #do this by iterating over the dataframe with the iterrows() function.. however since it returns a tuple you must unpack
  32. # create two empty lists, one for the opening line and one for the closing line
  33. def makeplot(teamdf,myteam):
  34.     lineopen=[]
  35.     lineclose=[]
  36.     for k,v in teamdf.iterrows():
  37.         if v.awayteam==myteam:
  38.             lineopen.append(v.awayopener)
  39.             lineclose.append(v.awaypinny)
  40.         else:
  41.             lineopen.append(v.homeopener)
  42.             lineclose.append(v.homepinny)
  43.     p=figure(plot_width=700,plot_height=700,title=myteam)
  44.     p.background_fill_color="red"
  45.     p.background_fill_alpha=0.8
  46.     p.background_fill_alpha=0.4
  47.     p.title.text_font_style="bold"
  48.     p.outline_line_color="navy"
  49.     p.outline_line_width=5
  50.     p.outline_line_alpha=0.3
  51.     x=[i for i in range(1,83)]
  52.     p.circle(x,lineopen,fill_color="green",size=10,fill_alpha=0.8,name="openinglines",legend=myteam+" Opening Lines")
  53.     ####################################
  54.     p.circle(x,lineclose,fill_color="red",size=10,fill_alpha=0.8,name="closinglines",legend=myteam+" Pinnacle Closing Line")
  55.     return p
  56. graph=makeplot(teamdf,myteam)
  57.  
  58. ts1=column(graph)
  59. select=Select(value=myteam, options=teamslist)
  60. widget=widgetbox(select)
  61. layout=column(widget,ts1)
  62.  
  63. def ticker1_change(attrname, old, new):
  64.     global layout, ts1, newteam, picked, graph
  65.     newteam=select.value
  66.     picked=makedf(newteam)
  67.     graph=makeplot(picked,newteam)
  68.     ts1=column(graph)
  69.     layout=column(widget,ts1)
  70.     curdoc().add_root(layout)
  71.  
  72.  
  73.  
  74. select.on_change('value',ticker1_change)
  75.  
  76. curdoc().add_root(layout)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement