#NEWLINE# def load_birth_names( ) : #NEWLINE# #INDENT# 'Loading birth name dataset from a zip file in the repo' #NEWLINE# with gzip . open ( os . path . join ( DATA_FOLDER , 'birth_names.json.gz' ) ) as f : #NEWLINE# #INDENT# pdf = pd . read_json ( f ) #NEWLINE# #UNINDENT# pdf . ds = pd . to_datetime ( pdf . ds , unit = 'ms' ) #NEWLINE# pdf . to_sql ( 'birth_names' , db . engine , if_exists = 'replace' , chunksize = 500 , dtype = { #NEWLINE# #INDENT# 'ds' : DateTime , #NEWLINE# 'gender' : String ( 16 ) , #NEWLINE# 'state' : String ( 10 ) , #NEWLINE# 'name' : String ( 255 ) , #NEWLINE# #UNINDENT# } , index = False ) #NEWLINE# l = [ ] #NEWLINE# print ( 'Done loading table!' ) #NEWLINE# print ( ( '-' * 80 ) ) #NEWLINE# print ( 'Creating table reference' ) #NEWLINE# obj = db . session . query ( TBL ) . filter_by ( table_name = 'birth_names' ) . first ( ) #NEWLINE# if ( not obj ) : #NEWLINE# #INDENT# obj = TBL ( table_name = 'birth_names' ) #NEWLINE# #UNINDENT# obj . main_dttm_col = 'ds' #NEWLINE# obj . database = get_or_create_db ( db . session ) #NEWLINE# obj . is_featured = True #NEWLINE# db . session . merge ( obj ) #NEWLINE# db . session . commit ( ) #NEWLINE# obj . fetch_metadata ( ) #NEWLINE# tbl = obj #NEWLINE# defaults = { #NEWLINE# #INDENT# 'compare_lag' : '10' , #NEWLINE# 'compare_suffix' : 'o10Y' , #NEWLINE# 'datasource_id' : '1' , #NEWLINE# 'datasource_name' : 'birth_names' , #NEWLINE# 'datasource_type' : 'table' , #NEWLINE# 'flt_op_1' : 'in' , #NEWLINE# 'limit' : '25' , #NEWLINE# 'granularity' : 'ds' , #NEWLINE# 'groupby' : [ ] , #NEWLINE# 'metric' : 'sum__num' , #NEWLINE# 'metrics' : [ 'sum__num' ] , #NEWLINE# 'row_limit' : config . get ( 'ROW_LIMIT' ) , #NEWLINE# 'since' : '100 years ago' , #NEWLINE# 'until' : 'now' , #NEWLINE# 'viz_type' : 'table' , #NEWLINE# 'where' : '' , #NEWLINE# 'markup_type' : 'markdown' , #NEWLINE# #UNINDENT# } #NEWLINE# print ( 'Creating some slices' ) #NEWLINE# slices = [ Slice ( slice_name = 'Girls' , viz_type = 'table' , datasource_type = 'table' , table = tbl , params = get_slice_json ( defaults , groupby = [ 'name' ] , flt_col_1 = 'gender' , flt_eq_1 = 'girl' , row_limit = 50 ) ) , Slice ( slice_name = 'Boys' , viz_type = 'table' , datasource_type = 'table' , table = tbl , params = get_slice_json ( defaults , groupby = [ 'name' ] , flt_col_1 = 'gender' , flt_eq_1 = 'boy' , row_limit = 50 ) ) , Slice ( slice_name = 'Participants' , viz_type = 'big_number' , datasource_type = 'table' , table = tbl , params = get_slice_json ( defaults , viz_type = 'big_number' , granularity = 'ds' , compare_lag = '5' , compare_suffix = 'over 5Y' ) ) , Slice ( slice_name = 'Genders' , viz_type = 'pie' , datasource_type = 'table' , table = tbl , params = get_slice_json ( defaults , viz_type = 'pie' , groupby = [ 'gender' ] ) ) , Slice ( slice_name = 'Genders by State' , viz_type = 'dist_bar' , datasource_type = 'table' , table = tbl , params = get_slice_json ( defaults , flt_eq_1 = 'other' , viz_type = 'dist_bar' , metrics = [ 'sum__sum_girls' , 'sum__sum_boys' ] , groupby = [ 'state' ] , flt_op_1 = 'not in' , flt_col_1 = 'state' ) ) , Slice ( slice_name = 'Trends' , viz_type = 'line' , datasource_type = 'table' , table = tbl , params = get_slice_json ( defaults , viz_type = 'line' , groupby = [ 'name' ] , granularity = 'ds' , rich_tooltip = 'y' , show_legend = 'y' ) ) , Slice ( slice_name = 'Title' , viz_type = 'markup' , datasource_type = 'table' , table = tbl , params = get_slice_json ( defaults , viz_type = 'markup' , markup_type = 'html' , code = '<div style="text-align:center">\n    <h1>Birth Names Dashboard</h1>\n    <p>\n        The source dataset came from\n        <a href="https://github.com/hadley/babynames">[here]</a>\n    </p>\n    <img src="http://monblog.system-linux.net/image/tux/baby-tux_overlord59-tux.png">\n</div>\n' ) ) , Slice ( slice_name = 'Name Cloud' , viz_type = 'word_cloud' , datasource_type = 'table' , table = tbl , params = get_slice_json ( defaults , viz_type = 'word_cloud' , size_from = '10' , series = 'name' , size_to = '70' , rotation = 'square' , limit = '100' ) ) , Slice ( slice_name = 'Pivot Table' , viz_type = 'pivot_table' , datasource_type = 'table' , table = tbl , params = get_slice_json ( defaults , viz_type = 'pivot_table' , metrics = [ 'sum__num' ] , groupby = [ 'name' ] , columns = [ 'state' ] ) ) , Slice ( slice_name = 'Number of Girls' , viz_type = 'big_number_total' , datasource_type = 'table' , table = tbl , params = get_slice_json ( defaults , viz_type = 'big_number_total' , granularity = 'ds' , flt_col_1 = 'gender' , flt_eq_1 = 'girl' , subheader = 'total female participants' ) ) ] #NEWLINE# for slc in slices : #NEWLINE# #INDENT# merge_slice ( slc ) #NEWLINE# #UNINDENT# print ( 'Creating a dashboard' ) #NEWLINE# dash = db . session . query ( Dash ) . filter_by ( dashboard_title = 'Births' ) . first ( ) #NEWLINE# if ( not dash ) : #NEWLINE# #INDENT# dash = Dash ( ) #NEWLINE# #UNINDENT# js = textwrap . dedent ( '        [\n            {\n                "size_y": 4,\n                "size_x": 2,\n                "col": 8,\n                "slice_id": "85",\n                "row": 7\n            },\n            {\n                "size_y": 4,\n                "size_x": 2,\n                "col": 10,\n                "slice_id": "86",\n                "row": 7\n            },\n            {\n                "size_y": 2,\n                "size_x": 2,\n                "col": 1,\n                "slice_id": "87",\n                "row": 1\n            },\n            {\n                "size_y": 2,\n                "size_x": 2,\n                "col": 3,\n                "slice_id": "88",\n                "row": 1\n            },\n            {\n                "size_y": 3,\n                "size_x": 7,\n                "col": 5,\n                "slice_id": "89",\n                "row": 4\n            },\n            {\n                "size_y": 4,\n                "size_x": 7,\n                "col": 1,\n                "slice_id": "90",\n                "row": 7\n            },\n            {\n                "size_y": 3,\n                "size_x": 3,\n                "col": 9,\n                "slice_id": "91",\n                "row": 1\n            },\n            {\n                "size_y": 3,\n                "size_x": 4,\n                "col": 5,\n                "slice_id": "92",\n                "row": 1\n            },\n            {\n                "size_y": 4,\n                "size_x": 4,\n                "col": 1,\n                "slice_id": "93",\n                "row": 3\n            }\n        ]\n        ' ) #NEWLINE# l = json . loads ( js ) #NEWLINE# for ( i , pos ) in enumerate ( l ) : #NEWLINE# #INDENT# pos [ 'slice_id' ] = str ( slices [ i ] . id ) #NEWLINE# #UNINDENT# dash . dashboard_title = 'Births' #NEWLINE# dash . position_json = json . dumps ( l , indent = 4 ) #NEWLINE# dash . slug = 'births' #NEWLINE# dash . slices = slices [ : ( - 1 ) ] #NEWLINE# db . session . merge ( dash ) #NEWLINE# db . session . commit ( )