forked from sd16spring/InteractiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathview_pandas.py
More file actions
128 lines (110 loc) · 4.31 KB
/
view_pandas.py
File metadata and controls
128 lines (110 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""
This part of the code comprises the view portion of model-view-controller.
It takes in a model, which is a pandas dataframe with information about resources for queer youth in MA.
It outputs an interactive map using Bokeh.
@author: Louise Nielsen and Apurva Raman
nielsenlouise@github.com, apurvaraman@github.com
"""
from bokeh.models.widgets import CheckboxButtonGroup
from bokeh.models import (GMapPlot,
GMapOptions,
DataRange1d,
Circle,
PanTool,
WheelZoomTool,
BoxSelectTool,
HoverTool,
Legend)
from bokeh.io import show, output_file
import model_pandas as mdl
# let's define glyphs
red_glyph = Circle(size=15, fill_color='red', fill_alpha=.8)
class MakePlot(object):
"""Draws a plot. View does everything else, this is just there so that the
plot does not have to be redrawn every time something is filtered.
"""
def __init__(self, plot=None, map_type='roadmap', title='Resources'):
self.map_type = map_type
self.title = title
if plot is None:
map_options = GMapOptions(lat=42.3601, lng=-71.0589,
map_type=self.map_type, zoom=9)
plot = GMapPlot(x_range=DataRange1d(),
y_range=DataRange1d(),
map_options=map_options,
title=title)
plot.add_tools(PanTool(),
WheelZoomTool(),
BoxSelectTool(),
HoverTool())
self.plot = plot
class View(object):
"""Puts information from Model() on top of the nice gmap it got from MakePlot().
"""
def __init__(self, filename='test.csv', model=None, plot=None, button=None):
self.filename = filename
if model is None:
self.model = mdl.Model(self.filename)
else:
self.model = model
if plot is None:
x = MakePlot()
self.plot = x.plot
else:
self.plot = plot
if button is None:
button = CheckboxButtonGroup(labels=['were trying', 'did we succeed'])
self.button = button
else:
self.button = button
def marker(self):
"""Defines a marker (a circle glyph) to represent a resource on the map.
"""
self.model.set_color()
circle = Circle(x='lon', y='lat', size=15, fill_color='color', fill_alpha=.8)
self.plot.add_glyph(self.model.source, circle)
def legend(self):
"""Defines and adds to plot a legend that describes what type of
resource each color glyph maps to.
We did not have time to fully implement this before the due date.
The legend object would appear but would be empty.
"""
"""
red_glyph = self.plot.add_glyph(self.model.source, Circle(x='lon', y='lat', size=15, fill_color='red', fill_alpha=.8))
Legend.legends = [
('health', red_glyph)]
('support', green_glyph),
('housing', blue_glyph),
('advocacy', purple_glyph),
('legal', cyan_glyph)]
legend = Legend(location='bottom_left')
self.plot.add_layout(legend)
"""
pass # TODO: Implement this
def hover_tool(self):
"""Makes the hover tool! Woot!
"""
hover = self.plot.select_one(HoverTool)
hover.point_policy = 'follow_mouse'
hover.tooltips = [('Name', '@name'),
('Address', '@address'),
('Type', '@category')]
def buttons(self):
"""Makes buttons appear.
Right now we're struggling with this because we probably have to use
either a server or callbacks, one of which is being difficult and also
one that involves a JS snippet.
"""
pass # TODO: Implement this.
def show_plot(self):
"""Outputs and displays a plot on a static webpage.
"""
self.marker()
self.hover_tool()
# self.legend()
self.buttons()
output_file('resources_plot.html')
show(self.plot)
if __name__ == '__main__':
thing = View()
thing.show_plot()