<s> import uuid , json , warnings <EOL> import param <EOL> from . . widgets import NdWidget , SelectionWidget , ScrubberWidget <EOL> try : <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . simplefilter ( "<STR_LIT:ignore>" ) <EOL> from matplotlib . backends . backend_nbagg import CommSocket <EOL> except ImportError : <EOL> CommSocket = object <EOL> class WidgetCommSocket ( CommSocket ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , manager ) : <EOL> self . supports_binary = None <EOL> self . manager = manager <EOL> self . uuid = str ( uuid . uuid4 ( ) ) <EOL> self . html = "<STR_LIT>" % self . uuid <EOL> def start ( self ) : <EOL> try : <EOL> from ipykernel . comm import Comm <EOL> except : <EOL> from IPython . kernel . comm import Comm <EOL> try : <EOL> self . comm = Comm ( '<STR_LIT>' , data = { '<STR_LIT:id>' : self . uuid } ) <EOL> except AttributeError : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . comm . on_msg ( self . on_message ) <EOL> self . comm . on_close ( lambda close_message : self . manager . clearup_closed ( ) ) <EOL> class MPLWidget ( NdWidget ) : <EOL> CDN = param . Dict ( default = dict ( NdWidget . CDN , mpld3 = '<STR_LIT>' , <EOL> d3 = '<STR_LIT>' ) ) <EOL> extensionjs = param . String ( default = '<STR_LIT>' , doc = """<STR_LIT>""" ) <EOL> template = param . String ( default = '<STR_LIT>' ) <EOL> def __init__ ( self , plot , renderer = None , ** params ) : <EOL> super ( MPLWidget , self ) . __init__ ( plot , renderer , ** params ) <EOL> if self . renderer . mode == '<STR_LIT>' : <EOL> self . cached = False <EOL> self . initialize_connection ( plot ) <EOL> def _plot_figure ( self , idx ) : <EOL> with self . renderer . state ( ) : <EOL> self . plot . update ( idx ) <EOL> if self . renderer . mode == '<STR_LIT>' : <EOL> figure_format = '<STR_LIT>' <EOL> elif self . renderer . fig == '<STR_LIT>' : <EOL> figure_format = self . renderer . params ( '<STR_LIT>' ) . objects [ <NUM_LIT:0> ] <EOL> else : <EOL> figure_format = self . renderer . fig <EOL> return self . renderer . html ( self . plot , figure_format ) <EOL> def update ( self , key ) : <EOL> if self . plot . dynamic == '<STR_LIT>' and not isinstance ( key , int ) : <EOL> key = tuple ( dim . values [ k ] if dim . values else k <EOL> for dim , k in zip ( self . mock_obj . kdims , tuple ( key ) ) ) <EOL> if self . renderer . mode == '<STR_LIT>' : <EOL> if not self . manager . _shown : <EOL> self . comm . start ( ) <EOL> self . manager . add_web_socket ( self . comm ) <EOL> self . manager . _shown = True <EOL> fig = self . plot [ key ] <EOL> fig . canvas . draw_idle ( ) <EOL> return '<STR_LIT>' <EOL> frame = self . _plot_figure ( key ) <EOL> if self . renderer . mode == '<STR_LIT>' : <EOL> frame = self . encode_frames ( { <NUM_LIT:0> : frame } ) <EOL> return frame <EOL> def get_frames ( self ) : <EOL> if self . renderer . mode == '<STR_LIT>' : <EOL> self . manager . display_js ( ) <EOL> frames = { <NUM_LIT:0> : self . comm . html } <EOL> elif self . embed : <EOL> return super ( MPLWidget , self ) . get_frames ( ) <EOL> else : <EOL> frames = { <NUM_LIT:0> : self . _plot_figure ( <NUM_LIT:0> ) } <EOL> return self . encode_frames ( frames ) <EOL> def encode_frames ( self , frames ) : <EOL> if self . export_json : <EOL> self . save_json ( frames ) <EOL> return { } <EOL> elif not isinstance ( frames , dict ) : <EOL> pass <EOL> elif self . renderer . mode == '<STR_LIT>' : <EOL> import mpld3 <EOL> encoder = dict ( cls = mpld3 . _display . NumpyEncoder ) <EOL> frames = { idx : frame for idx , frame in frames . items ( ) } <EOL> frames = json . dumps ( frames , ** encoder ) <EOL> else : <EOL> frames = { idx : frame for idx , frame in frames . items ( ) } <EOL> return super ( MPLWidget , self ) . encode_frames ( frames ) <EOL> def initialize_connection ( self , plot ) : <EOL> plot . update ( <NUM_LIT:0> ) <EOL> self . manager = self . renderer . get_figure_manager ( plot . state ) <EOL> self . comm = WidgetCommSocket ( self . manager ) <EOL> class MPLSelectionWidget ( MPLWidget , SelectionWidget ) : <EOL> pass <EOL> class MPLScrubberWidget ( MPLWidget , ScrubberWidget ) : <EOL> pass </s>
