#NEWLINE# def to_data( df , data_type ) : #NEWLINE# #INDENT# 'Convert dataframe to data.\n    ' #NEWLINE# data_type = data_type . strip ( ) . lower ( ) #NEWLINE# if ( data_type == 'csv' ) : #NEWLINE# #INDENT# with io . StringIO ( ) as csv : #NEWLINE# #INDENT# df . to_csv ( csv ) #NEWLINE# return csv . getvalue ( ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( data_type == 'html' ) : #NEWLINE# #INDENT# return df . to_html ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( data_type == 'json' ) : #NEWLINE# #INDENT# df . insert ( 0 , 'idx' , df . index . map ( str ) ) #NEWLINE# return df . to_json ( orient = 'records' , force_ascii = False ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( data_type == 'table' ) : #NEWLINE# #INDENT# idx = df . index #NEWLINE# if ( ( not df . empty ) and isinstance ( idx [ 0 ] , datetime . datetime ) ) : #NEWLINE# #INDENT# idx = idx . map ( ( lambda x : x . strftime ( '%Y-%m-%d' ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# idx = idx . map ( str ) #NEWLINE# #UNINDENT# df . insert ( 0 , 'idx' , idx ) #NEWLINE# res = { #NEWLINE# #INDENT# 'columns' : list ( df . keys ( ) ) , #NEWLINE# 'values' : json . loads ( df . to_json ( orient = 'records' , force_ascii = False ) ) , #NEWLINE# 'metadata' : df . _metadata , #NEWLINE# #UNINDENT# } #NEWLINE# return json . dumps ( res )