<s> from threading import Thread <EOL> import datetime <EOL> import logging <EOL> import time <EOL> import numpy as np <EOL> import netCDF4 <EOL> import pandas as pd <EOL> from bokeh . plotting import vplot , hplot , cursession , curdoc , output_server , show <EOL> from bokeh . models . widgets import Button , Icon <EOL> from viz import climate_map , timeseries , legend , get_slice <EOL> data = netCDF4 . Dataset ( '<STR_LIT>' ) <EOL> t = data . variables [ '<STR_LIT>' ] <EOL> df = pd . read_csv ( '<STR_LIT>' , parse_dates = [ <NUM_LIT:0> ] ) <EOL> output_server ( "<STR_LIT>" ) <EOL> from bokeh . plotting import figure <EOL> year = <NUM_LIT> <EOL> month = <NUM_LIT:1> <EOL> years = [ str ( x ) for x in np . arange ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> ) ] <EOL> months = [ str ( x ) for x in np . arange ( <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT:1> ) ] <EOL> months_str = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> month_str = months_str [ month - <NUM_LIT:1> ] <EOL> date = datetime . date ( year , month , <NUM_LIT:1> ) <EOL> df [ '<STR_LIT>' ] = pd . rolling_mean ( df [ '<STR_LIT>' ] , <NUM_LIT:12> ) <EOL> df = df . fillna ( <NUM_LIT:0> ) <EOL> title = figure ( width = <NUM_LIT> , height = <NUM_LIT:100> , x_range = ( <NUM_LIT:0> , <NUM_LIT> ) , y_range = ( <NUM_LIT:0> , <NUM_LIT:100> ) , toolbar_location = None , <EOL> x_axis_type = None , y_axis_type = None , outline_line_color = "<STR_LIT>" , tools = "<STR_LIT>" , min_border = <NUM_LIT:0> ) <EOL> title . text ( x = <NUM_LIT> , y = <NUM_LIT:5> , text = [ month_str ] , text_font_size = '<STR_LIT>' , text_color = '<STR_LIT>' , name = "<STR_LIT>" , text_font = "<STR_LIT>" ) <EOL> title . text ( x = <NUM_LIT> , y = <NUM_LIT:5> , text = [ str ( year ) ] , text_font_size = '<STR_LIT>' , text_color = '<STR_LIT>' , name = "<STR_LIT>" , text_font = "<STR_LIT>" ) <EOL> climate_map = climate_map ( ) <EOL> timeseries = timeseries ( ) <EOL> legend = legend ( ) <EOL> play = True <EOL> def play_handler ( ) : <EOL> print ( "<STR_LIT>" ) <EOL> global play <EOL> play = True <EOL> def stop_handler ( ) : <EOL> print ( "<STR_LIT>" ) <EOL> global play <EOL> play = False <EOL> button_start = Button ( label = "<STR_LIT>" , type = "<STR_LIT:success>" ) <EOL> button_start . on_click ( play_handler ) <EOL> button_stop = Button ( label = "<STR_LIT>" , type = "<STR_LIT>" ) <EOL> button_stop . on_click ( stop_handler ) <EOL> controls = hplot ( button_start , button_stop ) <EOL> timeseries . circle ( x = [ date ] , y = [ df [ df . datetime == date ] . moving_average ] , size = <NUM_LIT:8> , name = "<STR_LIT>" ) <EOL> map_legend = hplot ( climate_map , legend ) <EOL> layout = vplot ( controls , title , map_legend , timeseries ) <EOL> show ( layout ) <EOL> renderer = climate_map . select ( dict ( name = "<STR_LIT:image>" ) ) <EOL> ds = renderer [ <NUM_LIT:0> ] . data_source <EOL> month_renderer = title . select ( dict ( name = "<STR_LIT>" ) ) <EOL> month_ds = month_renderer [ <NUM_LIT:0> ] . data_source <EOL> year_renderer = title . select ( dict ( name = "<STR_LIT>" ) ) <EOL> year_ds = year_renderer [ <NUM_LIT:0> ] . data_source <EOL> timeseries_renderer = timeseries . select ( dict ( name = "<STR_LIT>" ) ) <EOL> timeseries_ds = timeseries_renderer [ <NUM_LIT:0> ] . data_source <EOL> def should_play ( ) : <EOL> """<STR_LIT>""" <EOL> global play <EOL> while True : <EOL> if play : <EOL> return True <EOL> else : <EOL> time . sleep ( <NUM_LIT> ) <EOL> def background_thread ( ds , year_ds , month_ds , timeseries_ds ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> while True : <EOL> for year_index in np . arange ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> ) : <EOL> year_ds . data [ "<STR_LIT:text>" ] = [ str ( year_index ) ] <EOL> for month_index in np . arange ( <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT:1> ) : <EOL> if should_play ( ) : <EOL> month_ds . data [ "<STR_LIT:text>" ] = [ months_str [ month_index - <NUM_LIT:1> ] ] <EOL> image = get_slice ( t , year_index , month_index ) <EOL> date = datetime . date ( year_index , month_index , <NUM_LIT:1> ) <EOL> timeseries_ds . data [ "<STR_LIT:x>" ] = [ date ] <EOL> timeseries_ds . data [ "<STR_LIT:y>" ] = [ df [ df . datetime == date ] . moving_average ] <EOL> ds . data [ "<STR_LIT:image>" ] = [ image ] <EOL> cursession ( ) . store_objects ( ds , year_ds , month_ds , timeseries_ds ) <EOL> time . sleep ( <NUM_LIT:0.5> ) <EOL> time . sleep ( <NUM_LIT:0.5> ) <EOL> except : <EOL> logger . exception ( "<STR_LIT>" ) <EOL> raise <EOL> Thread ( target = background_thread , args = ( ds , year_ds , month_ds , timeseries_ds ) ) . start ( ) <EOL> cursession ( ) . poll_document ( curdoc ( ) , <NUM_LIT> ) </s>
