def get_data_with_countries(wat_stats_df, san_stats_df, year=1990):
    countries_df = get_countries()

    # Get data for year, Merge water & sanitation
    wat_stats_df = wat_stats_df[wat_stats_df.year == year]
    san_stats_df = san_stats_df[san_stats_df.year == year]
    wat_san_df = wat_stats_df.merge(san_stats_df)

    # Merge the countries and stats together
    merged_df = countries_df.merge(wat_san_df, how='left')
    merged_df = merged_df.fillna(value=-99)

    # Color it
    colored_df = color_data(merged_df)
    colored_df['year'] = year

    # Otherwise things are sad!
    colored_df.columns = colored_df.columns.astype('str')
    return colored_df def get_wat_stats_all_years():
    wat_stats_df = get_stats_from_model('WNTI_%', 'wat_value')
    return wat_stats_df def get_san_stats_all_years():
    san_stats_df = get_stats_from_model('SNTI_%', 'san_value')
    return san_stats_df def get_line_data(country_name):
    wat_stats = get_line_stats_from_model('WNTI_%', country_name, 'wat_value')
    san_stats = get_line_stats_from_model('SNTI_%', country_name, 'san_value')
    wat_san = wat_stats.merge(san_stats, on='year')
    return wat_san def construct_water_map(source):
    return construct_map(source, fill_string='wat_color') def construct_water_map_tools(source):
    plot = construct_water_map(source)
    tooltips = "<span class='tooltip-text year'>@year</span>"
    tooltips += "<span class='tooltip-text country'>@name</span>"
    tooltips += "<span class='tooltip-text value'>@wat_value %</span>"
    plot.add_tools(HoverTool(tooltips=tooltips))
    plot.add_tools(TapTool())
    return plot def construct_san_map(source):
    return construct_map(source, fill_string='san_color', selected_color=DARK_GRAY)  # nopep8 def construct_san_map_tools(source):
    plot = construct_san_map(source)
    tooltips = "<span class='tooltip-text year'>@year</span>"
    tooltips += "<span class='tooltip-text country'>@name</span>"
    tooltips += "<span class='tooltip-text value'>@san_value %</span>"
    plot.add_tools(HoverTool(tooltips=tooltips))
    plot.add_tools(TapTool())
    return plot def construct_water_line(source):
    plot = construct_line(source, 'wat_value', BLUE)
    return plot def construct_san_line(source):
    plot = construct_line(source, 'san_value', GREEN)
    return plot def construct_water_text(source):
    plot = construct_text_box(source, 'wat_value', 'wat_color', BLUE)
    return plot def construct_san_text(source):
    plot = construct_text_box(source, 'san_value', 'san_color', GREEN)
    return plot def construct_key(palette):
    xdr = Range1d(0, 250)
    ydr = Range1d(0, 50)

    plot = Plot(
        x_range=xdr,
        y_range=ydr,
        title="",
        plot_width=250,
        plot_height=50,
        min_border=0,
        **PLOT_FORMATS
    )

    for index, color in enumerate(palette):
        width = 19
        rect = Rect(
            x=((width * index) + 40), y=40,
            width=width, height=10,
            fill_color=color, line_color='white'
        )
        plot.add_glyph(rect)

    zero = Text(x=30, y=15, text=['0 %'], **FONT_PROPS_SM)
    hundred = Text(x=190, y=15, text=['100 %'], **FONT_PROPS_SM)
    plot.add_glyph(zero)
    plot.add_glyph(hundred)

    return plot def layout_components(map_plot, line_plot, text_box, key):
    detail = vplot(text_box, line_plot, key)
    mapbox = vplot(map_plot)
    composed = hplot(mapbox, detail)
    return composed BLUE = WATER_COLOR_RANGE[5] GREEN = "#43C4AD" WATER_COLOR_RANGE = ["#8c9494", "#8398a2", "#7c9baa", "#73a1b4", "#6aa6bd", "#62abc7", "#5aafd0", "#52b4d9", "#49bae4", "#3fc0f0"] SANITATION_COLOR_RANGE = ["#d45500", "#da670f", "#eb7e1f", "#eb941f", "#ebb01f", "#f2c83d", "#d3cc4f", "#86c26f", "#4db181", "#15b598"]
from bokeh.models import (
    ColumnDataSource,
)
from bokeh.models.widgets import Tabs, Panel, Slider
from bokeh.plotting import vplot, hplot
from .map_data import (
    get_data_with_countries,
    get_wat_stats_all_years,
    get_san_stats_all_years,
    get_line_data,
)
from .water_map import (
    construct_water_map,
    construct_water_map_tools,
    construct_san_map,
    construct_san_map_tools,
    construct_water_line,
    construct_san_line,
    construct_water_text,
    construct_san_text,
    construct_key,
    layout_components,
)
from .chart_constants import (
    BLUE,
    GREEN,
    WATER_COLOR_RANGE,
    SANITATION_COLOR_RANGE,
)




wat_stats = get_wat_stats_all_years()
san_stats = get_san_stats_all_years()


def make_washmap_map():
    data = get_data_with_countries(wat_stats, san_stats)
    source = ColumnDataSource(data)
    wat_map = construct_water_map(source)
    wat_key = construct_key(WATER_COLOR_RANGE)
    return hplot(vplot(wat_map), vplot(wat_key))


def make_washmap_map_tools():
    data = get_data_with_countries(wat_stats, san_stats)
    source = ColumnDataSource(data)
    wat_map = construct_water_map_tools(source)
    wat_key = construct_key(WATER_COLOR_RANGE)
    return hplot(vplot(wat_map), vplot(wat_key))


def make_washmap_map_tools_linked():
    data = get_data_with_countries(wat_stats, san_stats)
    source = ColumnDataSource(data)
    wat_map = construct_water_map_tools(source)
    wat_text = construct_water_text(source)
    wat_key = construct_key(WATER_COLOR_RANGE)
    return hplot(vplot(wat_map), vplot(wat_text, wat_key))


def make_washmap_map_tools_linked_tabbed():
    data = get_data_with_countries(wat_stats, san_stats)
    source = ColumnDataSource(data)
    source.selected = [30]
    wat_map = construct_water_map_tools(source)
    wat_text = construct_water_text(source)
    wat_key = construct_key(WATER_COLOR_RANGE)

    san_map = construct_san_map_tools(source)