'''
How to use this file:

have the data, main_data.csv, in the same folder with this file
type bokeh serve widgets.py in your terminal
open a web brower and go to localhost:5006, the page will show

'''

import pandas as pd
from bokeh.io import output_file, show, save
from bokeh.tile_providers import STAMEN_TONER
from bokeh.models import (
  GMapPlot, GMapOptions, ColumnDataSource, Circle, DataRange1d, PanTool, WheelZoomTool, BoxSelectTool, Patches
)
from bokeh.models import HoverTool
from bokeh.io import curdoc, output_file, show
from bokeh.layouts import widgetbox, layout, column
from bokeh.models.widgets import Button, TextInput, RadioButtonGroup, Select, Slider, Paragraph
from bokeh.plotting import figure, show, output_server

# create widgets
text_input = TextInput(value='Lisa')
select1 = Select(title="Bedroom number:", value="2", options=['2', '3', '4', '5'])
select2 = Select(title="Bathroom number:", value="2", options=['2', '3', '4', '5'])
select3 = Select(title="Sqft:", value="1000", options=['1000', '1500', '2000', '2500'])
select4 = Select(title="Built year:", value="2", options=['2', '3', '4', '5'])

button_1 = Button(label="Submit")
button_2 = Button(label="Reset")
output = Paragraph()

# Predict housing price using a linear model
def submit():
	coefficients = [100000, 10000, 1, 1]
	# value = select1.value * coefficients[0]  
	# + select2.value * coefficients[1] + \
 #    select3.value * coefficients[2] + select4.value*coefficients[3]
	output.text = 'Your house is predicted as, ' + select3.value + '$'

# Import dataset, the first sheet in the merged dataset
main_data = pd.read_csv("main_data.csv", sep = ",")

# Set the parameters and add tools for the map
map_options = GMapOptions(lat=47.5480, lng=-121.9836, map_type="roadmap", zoom=10)
plot = GMapPlot(x_range=DataRange1d(), y_range=DataRange1d(), map_options=map_options, plot_width=240, plot_height=360, title="Title")
plot.api_key = "AIzaSyAA875-_BZDwKoR4bMonQUJgLxYIIZ3wzw"
circle = Circle(x="lon", y="lat", size=2, fill_color="blue", fill_alpha=0.9, line_color=None)

# Subset data and update map based on inputs from the user
def update():
    sub_data = main_data[main_data.bedrooms == int(select1.value)]
    sub_data = sub_data[main_data.bedrooms == int(select2.value)]
    source = ColumnDataSource(data=dict(lat=sub_data['lat'], 
                                        lon=sub_data['long'],
                                        br=sub_data['bedrooms']))
    plot.add_glyph(source, circle)

my_hover = HoverTool()
my_hover.tooltips = [('Lattitude of the point', '@lat'), ('Longitude of the point', '@lon'), ('Number of Bedrooms', '@br')]
plot.add_tools(PanTool(), WheelZoomTool(), BoxSelectTool())
plot.add_tools(my_hover)

# Load initial map
update()

# Submit prediction and update map at each clicking of button 1
button_1.on_click(submit)
button_1.on_click(update)

sizing_mode = 'fixed'

#inputs = widgetbox(children=[select1, select2, select3, select4, button_1, button_2], sizing_mode=sizing_mode)
#lay_out = layout([inputs, plot], sizing_mode=sizing_mode)

lay_out = layout([[select1, select2, select3, select4], [button_1, button_2], [output, plot]])

curdoc().add_root(lay_out)

@@ -101,23 +101,21 @@
 h serve 
-widgets
+main1
 .py in y
@@ -676,16 +676,29 @@
 aragraph
+, Panel, Tabs
 %0Afrom bo
@@ -2887,31 +2887,8 @@
 e)%0A%0A
-sizing_mode = 'fixed'%0A%0A
 #inp
@@ -3055,16 +3055,17 @@
 _mode)%0A%0A
+#
 lay_out 
@@ -3083,34 +3083,36 @@
 select1, select2
+%5D
 , 
+%5B
 select3, select4
@@ -3111,16 +3111,22 @@
  select4
+, plot
 %5D, %5Bbutt
@@ -3153,16 +3153,443 @@
 tput
-, plot%5D%5D
+%5D%5D)%0A%0A# How to do tabs%0A%0Al1 = layout(children=%5B%5Bselect1, select2%5D, %5Bselect3, select4, plot%5D, %5Bbutton_1, button_2%5D, %5Boutput%5D%5D, sizing_mode='fixed')%0Al2 = layout(children=%5B%5Bselect1, select2%5D, %5Bselect3, select4, plot%5D, %5Bbutton_1, button_2%5D, %5Boutput%5D%5D, sizing_mode='fixed')%0A%0Atab1 = Panel(child=l1,title=%22Housing Price Prediction%22)%0Atab2 = Panel(child=l2,title=%22Bidding Price Prediction%22)%0A%0Atabs = Tabs(tabs=%5Btab1, tab2%5D)%0A%0Acurdoc().add_root(tabs
 )%0A%0Ac

Add tabs