自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用Python輕松開發(fā)數(shù)據(jù)庫(kù)取數(shù)下載工具

開發(fā) 后端
而在dash_table中還有很多高級(jí)特性,可以極大程度上豐富DataTable()所渲染網(wǎng)頁(yè)表格的交互能力,今天的文章作為「交互表格篇」的下篇,我們就來一起學(xué)習(xí)其中比較實(shí)用的一些特性。

 1 簡(jiǎn)介

這是我的系列教程「Python+Dash快速web應(yīng)用開發(fā)」的第十四期,在前兩期中,我們針對(duì)dash_table的自定義樣式、前后端分頁(yè)、單元格內(nèi)容編輯等特點(diǎn)展開了介紹。

而在dash_table中還有很多高級(jí)特性,可以極大程度上豐富DataTable()所渲染網(wǎng)頁(yè)表格的交互能力,今天的文章作為「交互表格篇」的下篇,我們就來一起學(xué)習(xí)其中比較實(shí)用的一些特性。

 

 

 

[[393870]]

 

圖1

2 dash_table的更多實(shí)用功能

2.1 更多表格交互特性

上一期文章最后我們學(xué)習(xí)了通過設(shè)置參數(shù)editable=True,使得渲染出的表格可以通過鼠標(biāo)雙擊進(jìn)行編輯,而dash_table除此之外,還有更多實(shí)用的交互能力:

2.1.1 按列排序

  • 普通單列排序

在DataTable()中,我們只需要設(shè)置參數(shù)sort_action='native',即可開啟列排序功能,此時(shí)每一列列名單元格內(nèi)都會(huì)出現(xiàn)部件供我們點(diǎn)擊切換排序方式:

app1.py

 

  1. import dash 
  2. import dash_table 
  3. import dash_bootstrap_components as dbc 
  4.  
  5. import seaborn as sns 
  6.  
  7. df = sns.load_dataset('iris'
  8.  
  9. app = dash.Dash(__name__) 
  10.  
  11. app.layout = dbc.Container( 
  12.     [ 
  13.         dash_table.DataTable( 
  14.             data=df.to_dict('records'), 
  15.             columns=[ 
  16.                 {'name'column'id'column
  17.                 for column in df.columns 
  18.             ], 
  19.             style_table={ 
  20.                 'height''500px'
  21.                 'overflow-y''auto' 
  22.             }, 
  23.             sort_action='native' 
  24.         ) 
  25.     ], 
  26.     style={ 
  27.         'margin-top''50px' 
  28.     } 
  29.  
  30. if __name__ == '__main__'
  31.     app.run_server(debug=True

 

圖片

 

 

圖2

  • 基于后端排序的多列排序

在DataTable()中設(shè)置sort_action='native'時(shí),對(duì)應(yīng)的是「按列排序」的前端模式,也即是數(shù)據(jù)一次性灌注到瀏覽器的前提下進(jìn)行排序,這種方式不僅不適合大型數(shù)據(jù)集,而且只支持「單列排序」。

而當(dāng)數(shù)據(jù)渲染方式為后端模式時(shí),我們通過設(shè)置參數(shù)sort_action='custom'以及sort_mode='multi',配合在回調(diào)中獲取屬性sort_by中記錄的參與排序的列名及升序降序方式,就可以實(shí)現(xiàn)多列排序。

我們?cè)谏弦黄诘腶pp2.py的基礎(chǔ)上修改得到下面的例子:

app2.py

 

  1. import dash 
  2. import dash_bootstrap_components as dbc 
  3. import dash_table 
  4. from dash.dependencies import Input, Output 
  5.  
  6. import seaborn as sns 
  7.  
  8. df = sns.load_dataset('iris'
  9. df.insert(0, '#', df.index
  10.  
  11. app = dash.Dash(__name__) 
  12.  
  13. app.layout = dbc.Container( 
  14.     [ 
  15.         dbc.Spinner( 
  16.             dash_table.DataTable( 
  17.                 id='dash-table'
  18.                 columns=[ 
  19.                     {'name'column'id'column
  20.                     for column in df.columns 
  21.                 ], 
  22.                 page_size=15,  # 設(shè)置單頁(yè)顯示15行記錄行數(shù) 
  23.                 page_action='custom'
  24.                 page_current=0, 
  25.                 style_header={ 
  26.                     'font-family''Times New Romer'
  27.                     'font-weight''bold'
  28.                     'text-align''center' 
  29.                 }, 
  30.                 style_data={ 
  31.                     'font-family''Times New Romer'
  32.                     'text-align''center' 
  33.                 }, 
  34.                 sort_action='custom'
  35.                 sort_mode='multi' 
  36.             ) 
  37.         ) 
  38.     ], 
  39.     style={ 
  40.         'margin-top''50px' 
  41.     } 
  42.  
  43.  
  44. @app.callback( 
  45.     [Output('dash-table''data'), 
  46.      Output('dash-table''page_count')], 
  47.     [Input('dash-table''page_current'), 
  48.      Input('dash-table''page_size'), 
  49.      Input('dash-table''sort_by')] 
  50. def refresh_page_data(page_current, page_size, sort_by): 
  51.  
  52.     if sort_by: 
  53.         return ( 
  54.             df 
  55.             .sort_values( 
  56.                 [col['column_id'for col in sort_by], 
  57.                 ascending=[ 
  58.                     col['direction'] == 'asc' 
  59.                     for col in sort_by 
  60.                 ] 
  61.             ) 
  62.             .iloc[page_current * page_size:(page_current + 1) * page_size] 
  63.             .to_dict('records'), 
  64.             1 + df.shape[0] // page_size 
  65.         ) 
  66.  
  67.     return ( 
  68.         df.iloc[page_current * page_size:(page_current + 1) * page_size].to_dict('records'), 
  69.         1 + df.shape[0] // page_size 
  70.     ) 
  71.  
  72.  
  73. if __name__ == '__main__'
  74.     app.run_server(debug=True

 

 

 

圖3

2.1.2 按列條件篩選

除了基于指定字段進(jìn)行排序之外,dash_table還支持列的條件篩選,設(shè)置filter_action="native",就可以開啟基礎(chǔ)的按列條件篩選功能,此時(shí)每一列表頭下都會(huì)多出供用戶輸入篩選條件的單元格:

app3.py

 

  1. import dash 
  2. import dash_table 
  3. import dash_bootstrap_components as dbc 
  4.  
  5. import seaborn as sns 
  6.  
  7. df = sns.load_dataset('iris'
  8.  
  9. app = dash.Dash(__name__) 
  10.  
  11. app.layout = dbc.Container( 
  12.     [ 
  13.         dash_table.DataTable( 
  14.             data=df.to_dict('records'), 
  15.             columns=[ 
  16.                 {'name'column'id'column
  17.                 for column in df.columns 
  18.             ], 
  19.             # 自定義條件篩選單元格樣式 
  20.             style_filter={ 
  21.                 'font-family''Times New Romer'
  22.                 'background-color''#e3f2fd' 
  23.             }, 
  24.             style_table={ 
  25.                 'height''500px'
  26.                 'overflow-y''auto' 
  27.             }, 
  28.             style_header={ 
  29.                 'font-family''Times New Romer'
  30.                 'font-weight''bold'
  31.                 'text-align''center' 
  32.             }, 
  33.             style_data={ 
  34.                 'font-family''Times New Romer'
  35.                 'text-align''center' 
  36.             }, 
  37.             filter_action="native" 
  38.         ) 
  39.     ], 
  40.     style={ 
  41.         'margin-top''50px' 
  42.     } 
  43.  
  44. if __name__ == '__main__'
  45.     app.run_server(debug=True

 

圖片

 

 

圖4

而dash_table中自帶的條件篩選語法很豐富,有條件的朋友可以前往https://dash.plotly.com/datatable/filtering了解更多。

而dash_table同樣可以實(shí)現(xiàn)后端篩選,和前面的后端排序類似,主要利用filter_query屬性的回調(diào)變化在后臺(tái)基于pandas等框架進(jìn)行數(shù)據(jù)篩選,比較簡(jiǎn)單,這里就不再贅述。

2.2 自帶的數(shù)據(jù)表格下載功能

dash_table還自帶了將當(dāng)前所渲染的表格內(nèi)容直接下載為csv或xlsx格式文件的簡(jiǎn)易功能,通過參數(shù)export_format設(shè)置導(dǎo)出的文件格式,但自帶的下載按鈕樣式比較丑,如果你對(duì)此有比較高的要求,還是建議結(jié)合之前的「上傳下載篇」自己設(shè)計(jì)相關(guān)功能:

 

 

 

 

 

圖5

2.3 凍結(jié)首行

通過設(shè)置參數(shù)fixed_rows={'headers': True},我們可以實(shí)現(xiàn)下滑查看表格的過程中,始終保持表頭被凍結(jié):

 

圖片

 

圖6

3 開發(fā)一個(gè)在線取數(shù)工具

在學(xué)習(xí)完今天的內(nèi)容之后,我們來結(jié)合之前「上傳下載篇」中提到的下載功能,來制作一個(gè)簡(jiǎn)單的對(duì)指定數(shù)據(jù)庫(kù)中的數(shù)據(jù)表進(jìn)行快速條件篩選并下載的工具,其中DataTable的derived_virtual_data屬性記錄了經(jīng)過排序、條件篩選等操作后當(dāng)前顯示的表格數(shù)據(jù):

 

 

圖7

app4.py

 

  1. import dash 
  2. import dash_bootstrap_components as dbc 
  3. import dash_core_components as dcc 
  4. import dash_html_components as html 
  5. import dash_table 
  6. from dash.dependencies import Input, Output 
  7.  
  8. from flask import send_from_directory 
  9.  
  10. import os 
  11. import uuid 
  12. from sqlalchemy import create_engine 
  13. import pandas as pd 
  14.  
  15. try: 
  16.     os.mkdir("downloads"
  17. except FileExistsError: 
  18.     pass 
  19.  
  20. engine = create_engine('mysql+pymysql://root:mysql@localhost/DASH'
  21.  
  22. app = dash.Dash(__name__) 
  23.  
  24.  
  25. @app.server.route('/download/<file>'
  26. def download(file): 
  27.     return send_from_directory('downloads', file) 
  28.  
  29.  
  30. app.layout = dbc.Container( 
  31.     [ 
  32.         dbc.Row( 
  33.             [ 
  34.                 dbc.Col(dbc.Button('更新數(shù)據(jù)表', id='refresh-tables', style={'width''100%'}), width=2), 
  35.                 dbc.Col(dcc.Dropdown(id='table-select', style={'width''100%'}), width=2) 
  36.             ] 
  37.         ), 
  38.         html.Hr(), 
  39.         dash_table.DataTable( 
  40.             id='dash-table'
  41.             editable=True
  42.             page_size=15, 
  43.             style_header={ 
  44.                 'font-family''Times New Romer'
  45.                 'font-weight''bold'
  46.                 'text-align''center' 
  47.             }, 
  48.             style_data={ 
  49.                 'font-family''Times New Romer'
  50.                 'text-align''center' 
  51.             }, 
  52.             style_data_conditional=[ 
  53.                 { 
  54.                     # 對(duì)選中狀態(tài)下的單元格進(jìn)行自定義樣式 
  55.                     "if": {"state""selected"}, 
  56.                     "background-color""#b3e5fc"
  57.                     "border""none" 
  58.                 }, 
  59.             ], 
  60.             filter_action="native" 
  61.         ), 
  62.         html.Br(), 
  63.         html.A(id='download-url', target="_blank"
  64.     ], 
  65.     style={ 
  66.         'margin-top''50px' 
  67.     } 
  68.  
  69.  
  70. @app.callback( 
  71.     Output('table-select''options'), 
  72.     Input('refresh-tables''n_clicks'
  73. def refresh_tables(n_clicks): 
  74.     if n_clicks: 
  75.         return [ 
  76.             { 
  77.                 'label'table
  78.                 'value'table 
  79.             } 
  80.             for table in pd.read_sql_query('SHOW TABLES', con=engine)['Tables_in_dash'
  81.         ] 
  82.  
  83.     return dash.no_update 
  84.  
  85.  
  86. @app.callback( 
  87.     [Output('dash-table''data'), 
  88.      Output('dash-table''columns')], 
  89.     Input('table-select''value'
  90. def render_dash_table(value): 
  91.     if value: 
  92.         df = pd.read_sql_table(value, con=engine) 
  93.  
  94.         return df.to_dict('records'), [ 
  95.             {'name'column'id'column
  96.             for column in df.columns 
  97.         ] 
  98.  
  99.     else
  100.         return [], [] 
  101.  
  102.  
  103. @app.callback( 
  104.     [Output("download-url""href"), 
  105.      Output("download-url""children")], 
  106.     [Input("dash-table""derived_virtual_data"), 
  107.      Input("dash-table""filter_query")], 
  108.     prevent_initial_call=True 
  109. def download_table(derived_virtual_data, filter_query): 
  110.     if derived_virtual_data: 
  111.         print(derived_virtual_data) 
  112.  
  113.         filename = f"output_{uuid.uuid1()}.xlsx" 
  114.  
  115.         pd.DataFrame(derived_virtual_data).to_excel("downloads/" + filename, index=False
  116.  
  117.         return "/download/" + filename, "下載當(dāng)前狀態(tài)表格" 
  118.  
  119.     return """" 
  120.  
  121.  
  122. if __name__ == '__main__'
  123.     app.run_server(debug=True

 

責(zé)任編輯:華軒 來源: Python大數(shù)據(jù)分析
相關(guān)推薦

2010-06-07 14:45:37

Linux下載工具

2012-06-01 09:29:56

HTML5

2010-06-03 10:53:13

Linux下載工具

2010-06-03 11:12:03

Linux下載工具

2010-06-03 11:28:35

2010-06-04 17:16:25

Linux 下載工具

2023-04-18 18:22:31

開源工具數(shù)據(jù)庫(kù)

2011-03-04 11:08:46

ADO.NET數(shù)據(jù)庫(kù)

2021-04-11 11:24:22

Python工具數(shù)據(jù)庫(kù)

2021-04-24 23:26:12

Python儀表盤存儲(chǔ)

2010-01-04 17:35:32

Silverlight

2011-10-12 11:22:51

LinuxClipGrab

2010-06-13 15:32:22

Linux 下載工具

2010-05-28 13:22:57

2010-05-28 12:59:18

Linux下載工具

2010-05-28 12:43:12

Linux下載工具

2023-06-08 08:46:37

Motrix下載工具

2010-02-04 15:17:48

Linux wget

2021-09-28 07:12:08

數(shù)倉(cāng)開發(fā)工具

2023-11-27 08:51:46

PythonRequests庫(kù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)