<s> from collections import defaultdict <EOL> from matplotlib . font_manager import FontProperties <EOL> from matplotlib . table import Table as mpl_Table <EOL> import param <EOL> from . element import ElementPlot <EOL> from ... core . util import safe_unicode <EOL> class TablePlot ( ElementPlot ) : <EOL> """<STR_LIT>""" <EOL> border = param . Number ( default = <NUM_LIT> , bounds = ( <NUM_LIT:0.0> , <NUM_LIT:0.5> ) , doc = """<STR_LIT>""" ) <EOL> float_precision = param . Integer ( default = <NUM_LIT:3> , doc = """<STR_LIT>""" ) <EOL> max_value_len = param . Integer ( default = <NUM_LIT:20> , doc = """<STR_LIT>""" ) <EOL> max_font_size = param . Integer ( default = <NUM_LIT:12> , doc = """<STR_LIT>""" ) <EOL> max_rows = param . Integer ( default = <NUM_LIT:15> , doc = """<STR_LIT>""" ) <EOL> font_types = param . Dict ( default = { '<STR_LIT>' : FontProperties ( weight = '<STR_LIT>' , <EOL> family = '<STR_LIT>' ) } , <EOL> doc = """<STR_LIT>""" ) <EOL> style_opts = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> _has_axes = False <EOL> def __init__ ( self , table , ** params ) : <EOL> super ( TablePlot , self ) . __init__ ( table , ** params ) <EOL> self . cell_values , self . cell_widths = self . _format_table ( ) <EOL> def _format_table ( self ) : <EOL> cell_values = defaultdict ( dict ) <EOL> cell_widths = defaultdict ( int ) <EOL> for key in self . keys : <EOL> frame = self . _get_frame ( key ) <EOL> if frame is None : <EOL> continue <EOL> summarize = frame . rows > self . max_rows <EOL> half_rows = self . max_rows // <NUM_LIT:2> <EOL> rows = min ( [ self . max_rows , frame . rows ] ) <EOL> for row in range ( rows ) : <EOL> adjusted_row = row <EOL> for col in range ( frame . cols ) : <EOL> if summarize and row == half_rows : <EOL> cell_text = "<STR_LIT>" <EOL> else : <EOL> if summarize and row > half_rows : <EOL> adjusted_row = ( frame . rows - self . max_rows + row ) <EOL> value = frame . pprint_cell ( adjusted_row , col ) <EOL> cell_text = self . pprint_value ( value ) <EOL> cell_values [ key ] [ ( row , col ) ] = cell_text <EOL> if len ( cell_text ) + <NUM_LIT:2> > cell_widths [ col ] : <EOL> cell_widths [ col ] = len ( cell_text ) + <NUM_LIT:2> <EOL> return cell_values , cell_widths <EOL> def pprint_value ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , float ) : <EOL> formatter = '<STR_LIT>' % self . float_precision <EOL> formatted = formatter . format ( value ) <EOL> elif isinstance ( value , str ) : <EOL> formatted = safe_unicode ( value ) <EOL> else : <EOL> formatted = str ( value ) <EOL> if len ( formatted ) > self . max_value_len : <EOL> return formatted [ : ( self . max_value_len - <NUM_LIT:3> ) ] + '<STR_LIT>' <EOL> else : <EOL> return formatted <EOL> def initialize_plot ( self , ranges = None ) : <EOL> element = self . hmap . last <EOL> axis = self . handles [ '<STR_LIT>' ] <EOL> axis . set_axis_off ( ) <EOL> size_factor = ( <NUM_LIT:1.0> - <NUM_LIT:2> * self . border ) <EOL> table = mpl_Table ( axis , bbox = [ self . border , self . border , <EOL> size_factor , size_factor ] ) <EOL> total_width = sum ( self . cell_widths . values ( ) ) <EOL> height = size_factor / element . rows <EOL> summarize = element . rows > self . max_rows <EOL> half_rows = self . max_rows / <NUM_LIT:2> <EOL> rows = min ( [ self . max_rows , element . rows ] ) <EOL> for row in range ( rows ) : <EOL> adjusted_row = row <EOL> for col in range ( element . cols ) : <EOL> if summarize and row > half_rows : <EOL> adjusted_row = ( element . rows - self . max_rows + row ) <EOL> cell_value = self . cell_values [ self . keys [ - <NUM_LIT:1> ] ] [ ( row , col ) ] <EOL> cellfont = self . font_types . get ( element . cell_type ( adjusted_row , col ) , None ) <EOL> width = self . cell_widths [ col ] / float ( total_width ) <EOL> font_kwargs = dict ( fontproperties = cellfont ) if cellfont else { } <EOL> table . add_cell ( row , col , width , height , text = cell_value , loc = '<STR_LIT>' , <EOL> ** font_kwargs ) <EOL> table . set_fontsize ( self . max_font_size ) <EOL> table . auto_set_font_size ( True ) <EOL> axis . add_table ( table ) <EOL> self . handles [ '<STR_LIT>' ] = table <EOL> return self . _finalize_axis ( self . keys [ - <NUM_LIT:1> ] ) <EOL> def update_handles ( self , key , axis , view , ranges , style ) : <EOL> table = self . handles [ '<STR_LIT>' ] <EOL> for coords , cell in table . get_celld ( ) . items ( ) : <EOL> value = self . cell_values [ key ] [ coords ] <EOL> cell . set_text_props ( text = value ) <EOL> table . set_fontsize ( self . max_font_size ) <EOL> table . auto_set_font_size ( True ) </s>
