<s> from . . charts import Chart <EOL> from flask import jsonify , request <EOL> class DataTable ( Chart ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , table_id , url , df , columns = { } , init_params = { } , <EOL> paging = False , searching = False , sortable = False , classname = "<STR_LIT>" , <EOL> route_func = None , ** kwargs ) : <EOL> opts = { <EOL> "<STR_LIT>" : init_params , <EOL> "<STR_LIT:id>" : table_id , <EOL> "<STR_LIT:url>" : url , <EOL> "<STR_LIT>" : classname , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : paging , <EOL> "<STR_LIT>" : searching , <EOL> "<STR_LIT>" : sortable <EOL> } <EOL> } <EOL> for k , v in list ( kwargs . items ( ) ) : <EOL> opts [ "<STR_LIT>" ] [ k ] = v <EOL> self . columns = columns <EOL> self . confidence = { } <EOL> for k , v in list ( self . columns . items ( ) ) : <EOL> if "<STR_LIT>" in v : <EOL> self . confidence [ k ] = v [ "<STR_LIT>" ] <EOL> if not route_func : <EOL> def get_data ( ) : <EOL> args = { } <EOL> for c in init_params : <EOL> if request . args . get ( c ) : <EOL> args [ c ] = request . args [ c ] <EOL> else : <EOL> args [ c ] = init_params [ c ] <EOL> return jsonify ( DataTable . to_json ( <EOL> self . apply_filters ( df , args ) , <EOL> self . columns , <EOL> confidence = self . confidence <EOL> ) ) <EOL> route_func = get_data <EOL> super ( DataTable , self ) . __init__ ( "<STR_LIT>" , opts , route_func ) <EOL> @ staticmethod <EOL> def format_row ( row , bounds , columns ) : <EOL> """<STR_LIT>""" <EOL> for c in columns : <EOL> if c not in row : <EOL> continue <EOL> if "<STR_LIT>" in columns [ c ] : <EOL> row [ c ] = columns [ c ] [ "<STR_LIT>" ] % row [ c ] <EOL> if c in bounds : <EOL> b = bounds [ c ] <EOL> row [ c ] = [ b [ "<STR_LIT>" ] , row [ b [ "<STR_LIT>" ] ] , row [ b [ "<STR_LIT>" ] ] , b [ "<STR_LIT>" ] ] <EOL> return row <EOL> @ staticmethod <EOL> def to_json ( df , columns , confidence = { } ) : <EOL> """<STR_LIT>""" <EOL> records = [ ] <EOL> display_cols = list ( columns . keys ( ) ) <EOL> if not display_cols : <EOL> display_cols = list ( df . columns ) <EOL> bounds = { } <EOL> for c in confidence : <EOL> bounds [ c ] = { <EOL> "<STR_LIT>" : df [ confidence [ c ] [ "<STR_LIT>" ] ] . min ( ) , <EOL> "<STR_LIT>" : df [ confidence [ c ] [ "<STR_LIT>" ] ] . max ( ) , <EOL> "<STR_LIT>" : confidence [ c ] [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : confidence [ c ] [ "<STR_LIT>" ] <EOL> } <EOL> labels = { } <EOL> for c in display_cols : <EOL> if "<STR_LIT:label>" in columns [ c ] : <EOL> labels [ c ] = columns [ c ] [ "<STR_LIT:label>" ] <EOL> else : <EOL> labels [ c ] = c <EOL> for i , row in df . iterrows ( ) : <EOL> row_ = DataTable . format_row ( row , bounds , columns ) <EOL> records . append ( { labels [ c ] : row_ [ c ] for c in display_cols } ) <EOL> return { <EOL> "<STR_LIT:data>" : records , <EOL> "<STR_LIT>" : [ { "<STR_LIT:data>" : labels [ c ] } for c in display_cols ] <EOL> } </s>
