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

60行Python代碼輕松搞定數(shù)據(jù)庫查詢 1秒找到需要的數(shù)據(jù)

開發(fā) 后端
學(xué)習(xí)Dash中渲染網(wǎng)頁靜態(tài)表格的常用方法,并在最后的例子中教大家如何配合Dash,簡簡單單編寫一個數(shù)據(jù)庫查詢應(yīng)用。

1 簡介

學(xué)習(xí)Dash中渲染網(wǎng)頁靜態(tài)表格的常用方法,并在最后的例子中教大家如何配合Dash,簡簡單單編寫一個數(shù)據(jù)庫查詢應(yīng)用~ 還特意在文末藏了驚喜哦!

[[391635]]

2 在Dash中渲染靜態(tài)表格

在Dash中渲染「靜態(tài)」表格,方法有很多,而我們今天要學(xué)習(xí)的方法,是配合之前文章介紹過的第三方拓展dash_bootstrap_components中的Table()部件,借助bootstrap的特性來快速創(chuàng)建美觀的「靜態(tài)」表格:

2.1 靜態(tài)表格的構(gòu)成

要學(xué)習(xí)如何基于Dash在前端中渲染出一張靜態(tài)表格,首先我們需要學(xué)習(xí)其元素構(gòu)成,Dash延續(xù)html中table標(biāo)簽相關(guān)概念,由Table()、Thead()、Tbody()、Tr()、Th()以及Td()等6個部件來構(gòu)成一張完整的表,先從一個簡單的例子出發(fā):

❝app1.py❞ 

  1. import dash 
  2. import dash_html_components as html 
  3. import dash_bootstrap_components as dbc 
  4.  
  5. app = dash.Dash(__name__) 
  6.  
  7. app.layout = html.Div( 
  8.     dbc.Container( 
  9.         dbc.Table
  10.             [ 
  11.                 html.Thead( 
  12.                     html.Tr( 
  13.                         [ 
  14.                             html.Th('第一列'), 
  15.                             html.Th('第二列'), 
  16.                         ] 
  17.                     ) 
  18.                 ), 
  19.                 html.Tbody( 
  20.                     [ 
  21.                         html.Tr( 
  22.                             [ 
  23.                                 html.Td('一行一列'), 
  24.                                 html.Td('一行二列'), 
  25.                             ] 
  26.                         ), 
  27.                         html.Tr( 
  28.                             [ 
  29.                                 html.Td('二行一列'), 
  30.                                 html.Td('二行二列'), 
  31.                             ] 
  32.                         ) 
  33.                     ] 
  34.                 ) 
  35.             ] 
  36.         ), 
  37.         style={ 
  38.             'margin-top''50px' # 設(shè)置頂部留白區(qū)域高度 
  39.         } 
  40.     ) 
  41.  
  42. if __name__ == '__main__'
  43.     app.run_server(debug=True

 

注意,我們這里使用到的Table()部件來自dash_bootstrap_components,而表格其余的構(gòu)成部件均來自Dash原生的dash_html_components庫,這些部件分別的作用如下:

「Table()」

Table()是一張靜態(tài)表格最外層的部件,而之所以選擇dash_bootstrap_components中的Table(),是因為其自帶了諸多實用參數(shù),常用的如下:

  • 「bordered」:bool型,用于設(shè)置是否「保留」表格外邊框線
  • 「borderless」:bool型,用于設(shè)置是否「刪除」表格內(nèi)部單元格框線
  • 「striped」:bool型,用于設(shè)置是否對數(shù)值行應(yīng)用「斑馬著色」方案,即相鄰行背景色不同
  • 「dark」:bool型,用于設(shè)置是否應(yīng)用「暗黑」主題
  • 「hover」:bool型,當(dāng)設(shè)置為True時,鼠標(biāo)懸浮于某行會有對應(yīng)的效果

通過上述參數(shù),我們就可以改變靜態(tài)表格的整體效果,譬如設(shè)置dark=True之后的app1.py效果如下:

「Thead()與Tbody()」

在部件Table()之下一級需要子元素Thead()與Tbody(),分別用于存放表頭信息以及表數(shù)值內(nèi)容信息。

「Tr()、Th()與Td()」

經(jīng)過前面Table()嵌套Thead()與Tbody()的過程之后,我們就可以分別開始在「表頭區(qū)域」和「數(shù)值區(qū)域」正式組織數(shù)據(jù)內(nèi)容。

既然是一張表格,那么還是要按照先行后列的網(wǎng)格方式組織內(nèi)容。而Tr()部件的作用就是作為行容器,其內(nèi)部嵌套的子元素則是表格中每個單元格位置上的元素。

其中在Thead()嵌套的Tr()內(nèi)部,需要使用Th()來設(shè)置每列的字段名稱,而在Tbody()嵌套的Tr()內(nèi)部,Td()與Th()都可以用來設(shè)置每個單元格的數(shù)值內(nèi)容,只不過Th()在表現(xiàn)單元格數(shù)值時有加粗效果:

❝app2.py❞ 

  1. import dash 
  2. import dash_html_components as html 
  3. import dash_bootstrap_components as dbc 
  4.  
  5. app = dash.Dash(__name__) 
  6.  
  7. app.layout = html.Div( 
  8.     dbc.Container( 
  9.         dbc.Table
  10.             [ 
  11.                 html.Thead( 
  12.                     html.Tr( 
  13.                         [ 
  14.                             html.Th('字段1'), 
  15.                             html.Th('字段2'
  16.                         ] 
  17.                     ) 
  18.                 ), 
  19.                 html.Tbody( 
  20.                     [ 
  21.                         html.Tr( 
  22.                             [ 
  23.                                 html.Th('1'), 
  24.                                 html.Td('test'
  25.                             ] 
  26.                         ), 
  27.                         html.Tr( 
  28.                             [ 
  29.                                 html.Th('2'), 
  30.                                 html.Td('test'
  31.                             ] 
  32.                         ), 
  33.                         html.Tr( 
  34.                             [ 
  35.                                 html.Td('3'), 
  36.                                 html.Td('test'
  37.                             ] 
  38.                         ) 
  39.                     ] 
  40.                 ) 
  41.             ], 
  42.             striped=True 
  43.         ), 
  44.         style={ 
  45.             'margin-top''50px'  # 設(shè)置頂部留白區(qū)域高度 
  46.         } 
  47.     ) 
  48.  
  49. if __name__ == '__main__'
  50.     app.run_server(debug=True

 

而Th()與Td()均有額外參數(shù)colSpan與rowSpan,可以傳入整數(shù),來實現(xiàn)橫向或縱向「合并單元格」的效果,譬如下面的例子:

❝app3.py❞ 

  1. import dash 
  2. import dash_html_components as html 
  3. import dash_bootstrap_components as dbc 
  4.  
  5. app = dash.Dash(__name__) 
  6.  
  7. app.layout = html.Div( 
  8.     dbc.Container( 
  9.         dbc.Table
  10.             [ 
  11.                 html.Thead( 
  12.                     html.Tr( 
  13.                         [ 
  14.                             html.Th('字段1'), 
  15.                             html.Th('字段2'), 
  16.                             html.Th('字段3'), 
  17.                             html.Th('字段4'), 
  18.                         ] 
  19.                     ) 
  20.                 ), 
  21.                 html.Tbody( 
  22.                     [ 
  23.                         html.Tr( 
  24.                             [ 
  25.                                 html.Th('1'), 
  26.                                 # style設(shè)置水平居中 
  27.                                 html.Td('colSpan=2', colSpan=2, style={'text-align''center'}), 
  28.                                 html.Td('test'), 
  29.                             ] 
  30.                         ), 
  31.                         html.Tr( 
  32.                             [ 
  33.                                 html.Th('2'), 
  34.                                 html.Td('test'), 
  35.                                 # style設(shè)置垂直居中 
  36.                                 html.Td('rowSpan=2', rowSpan=2, style={'vertical-align''middle'}), 
  37.                                 html.Td('test'
  38.                             ] 
  39.                         ), 
  40.                         html.Tr( 
  41.                             [ 
  42.                                 html.Th('3'), 
  43.                                 html.Td('test'), 
  44.                                 html.Td('test'
  45.                             ] 
  46.                         ) 
  47.                     ] 
  48.                 ) 
  49.             ], 
  50.             striped=True
  51.             bordered=True 
  52.         ), 
  53.         style={ 
  54.             'margin-top''50px'  # 設(shè)置頂部留白區(qū)域高度 
  55.         } 
  56.     ) 
  57.  
  58. if __name__ == '__main__'
  59.     app.run_server(debug=True

 

2.2 快速表格渲染

2.2.1 利用列表推導(dǎo)快速渲染靜態(tài)表格

通過前面的內(nèi)容,我們知曉了在Dash中如果渲染一張帶有樣式的靜態(tài)表格,而日常需求中,面對批量的數(shù)據(jù),我們當(dāng)然不可能手動編寫整張表對應(yīng)的代碼,對于數(shù)量較多的表格,我們可以配合Python中常用的列表推導(dǎo)來實現(xiàn)。

比如下面的例子:

❝app4.py❞ 

  1. import dash 
  2. import dash_html_components as html 
  3. import dash_bootstrap_components as dbc 
  4. import pandas as pd 
  5. import numpy as np 
  6.  
  7. fake_df = pd.DataFrame(np.random.rand(1000).reshape(200, 5)) 
  8. fake_df.rename(lambda s: f'字段{s}', axis=1, inplace=True) # 批量格式化列名 
  9.  
  10. app = dash.Dash(__name__) 
  11.  
  12. app.layout = html.Div( 
  13.     dbc.Container( 
  14.         dbc.Table
  15.             [ 
  16.                 html.Thead( 
  17.                     html.Tr( 
  18.                         [html.Th('行下標(biāo)', style={'text-align''center'})] + 
  19.                         [ 
  20.                             html.Th(column, style={'text-align''center'}) 
  21.                             for column in fake_df.columns 
  22.                         ] 
  23.                     ) 
  24.                 ), 
  25.                 html.Tbody( 
  26.                     [ 
  27.                         html.Tr( 
  28.                             [html.Th(f'#{idx}', style={'text-align''center'})] + 
  29.                             [ 
  30.                                html.Td(row[column], style={'text-align''center'}) 
  31.                                 for column in fake_df.columns 
  32.                             ] 
  33.                         ) 
  34.                         for idx, row in fake_df.iterrows() 
  35.                     ] 
  36.                 ) 
  37.             ], 
  38.             striped=True
  39.             bordered=True 
  40.         ), 
  41.         style={ 
  42.             'margin-top''50px'  # 設(shè)置頂部留白區(qū)域高度 
  43.         } 
  44.     ) 
  45.  
  46. if __name__ == '__main__'
  47.     app.run_server(debug=True

在生成表頭和每行內(nèi)容時應(yīng)用列表推導(dǎo),使得我們的代碼更加簡潔。

2.2.2 利用from_dataframe()快速渲染表格

上述的列表推導(dǎo)方式雖說已經(jīng)簡潔了很多,但dash_bootstrap_components還提供了Table.from_dataframe()方法,可以直接傳入pandas數(shù)據(jù)框來快速制作簡易的靜態(tài)表格。

它的樣式相關(guān)參數(shù)與dbc.Table()一致,缺點是自定義表格內(nèi)部元素樣式的自由度沒有前面列表推導(dǎo)高:

❝app5.py❞ 

  1. import dash 
  2. import dash_html_components as html 
  3. import dash_bootstrap_components as dbc 
  4. import pandas as pd 
  5. import numpy as np 
  6.  
  7. fake_df = pd.DataFrame(np.random.rand(1000).reshape(200, 5)) 
  8. fake_df.rename(lambda s: f'字段{s}', axis=1, inplace=True) # 批量格式化列名 
  9.  
  10. app = dash.Dash(__name__) 
  11.  
  12. app.layout = html.Div( 
  13.     dbc.Container( 
  14.         # 一行代碼渲染靜態(tài)表格 
  15.         dbc.Table.from_dataframe(fake_df, striped=True), 
  16.         style={ 
  17.             'margin-top''50px'  # 設(shè)置頂部留白區(qū)域高度 
  18.         } 
  19.     ) 
  20.  
  21. if __name__ == '__main__'
  22.     app.run_server(debug=True

 

3 自制簡易的數(shù)據(jù)庫查詢系統(tǒng)

在學(xué)習(xí)了今天的內(nèi)容之后,我們就可以創(chuàng)建很多以表格為主體內(nèi)容的web應(yīng)用,典型如數(shù)據(jù)庫查詢系統(tǒng),我們以Postgresql為例,配合pandas與sqlalchemy的相關(guān)功能,來快速打造一個簡單的數(shù)據(jù)庫查詢系統(tǒng)。

首先將本期附件中的所有數(shù)據(jù)表利用下面的代碼導(dǎo)入目標(biāo)數(shù)據(jù)庫中:

 

接著只需要配合Dash,短短的幾十行代碼就可以實現(xiàn)。

對應(yīng)代碼如下:

❝app6.py❞ 

  1. import dash 
  2. import dash_html_components as html 
  3. import dash_bootstrap_components as dbc 
  4. import dash_core_components as dcc 
  5. from dash.dependencies import Input, Output, State 
  6. import pandas as pd 
  7. from sqlalchemy import create_engine 
  8.  
  9. postgres_url = 'postgresql://postgres:填入你的密碼@localhost:5432/Dash' 
  10. engine = create_engine(postgres_url) 
  11.  
  12. app = dash.Dash(__name__) 
  13.  
  14. app.layout = html.Div( 
  15.     dbc.Container( 
  16.         [ 
  17.             dbc.Row( 
  18.                 [ 
  19.                     dbc.Col(dbc.Button('更新數(shù)據(jù)庫信息', id='refresh-db', style={'width''100%'}), width=2), 
  20.                     dbc.Col(dcc.Dropdown(id='db-table-names', placeholder='選擇庫中數(shù)據(jù)表', style={'width''100%'}), width=4), 
  21.                     dbc.Col(dbc.Button('查詢', id='query', style={'width''100%'}), width=1) 
  22.                 ] 
  23.             ), 
  24.             html.Hr(), 
  25.             dbc.Row( 
  26.                 [ 
  27.                     dbc.Col( 
  28.                         id='query-result' 
  29.                     ) 
  30.                 ] 
  31.             ) 
  32.         ], 
  33.         style={ 
  34.             'margin-top''50px'  # 設(shè)置頂部留白區(qū)域高度 
  35.         } 
  36.     ) 
  37.  
  38. @app.callback( 
  39.     Output('db-table-names''options'), 
  40.     Input('refresh-db''n_clicks'), 
  41.     prevent_initial_call=True 
  42. def query_data_records(n_clicks): 
  43.  
  44.         # 提取目標(biāo)表格并查詢其最多前500行記錄 
  45.         table_names = pd.read_sql_query("select tablename from pg_tables where schemaname='public'", con=engine) 
  46.         return [{'label'name'value'namefor name in table_names['tablename']] 
  47.  
  48. @app.callback( 
  49.     Output('query-result''children'), 
  50.     Input('query''n_clicks'), 
  51.     State('db-table-names''value'), 
  52.     prevent_initial_call=True 
  53. def refresh_table_names(n_clicks, value): 
  54.     if value: 
  55.         query_result = pd.read_sql_query(f'select * from {value} limit 500', con=engine) 
  56.  
  57.         return html.Div(dbc.Table.from_dataframe(query_result, striped=True), style={'height''600px''overflow''auto'}) 
  58.     else
  59.         return dash.no_update 
  60.  
  61. if __name__ == '__main__'
  62.     app.run_server(debug=True

以上就是本文的全部內(nèi)容,歡迎在評論區(qū)與我進(jìn)行討論。

 

責(zé)任編輯:未麗燕 來源: 今日頭條
相關(guān)推薦

2023-11-13 08:16:08

MySQL數(shù)據(jù)數(shù)據(jù)庫

2013-07-17 09:42:34

云計算數(shù)據(jù)庫NoSQL

2019-03-05 10:16:54

數(shù)據(jù)分區(qū)表SQLserver

2019-07-09 08:23:07

數(shù)據(jù)安全旅游網(wǎng)絡(luò)安全

2024-12-27 08:39:10

2024-09-09 16:50:21

2016-04-29 10:02:39

2023-12-01 15:50:46

2019-05-05 09:46:01

Python代碼神經(jīng)網(wǎng)絡(luò)

2024-07-17 08:29:20

2024-08-29 08:58:30

JPA編寫數(shù)據(jù)操

2010-02-03 16:46:07

Python特定數(shù)據(jù)庫

2021-10-28 09:42:38

代碼編碼開發(fā)

2016-09-09 01:07:06

數(shù)據(jù)中心容量規(guī)劃數(shù)據(jù)中心

2024-04-07 08:19:19

Oracle數(shù)據(jù)庫故障

2018-02-27 15:48:31

數(shù)據(jù)庫SQL鎖死

2023-12-13 07:59:04

2011-07-20 13:40:00

SQLite數(shù)據(jù)庫查詢數(shù)據(jù)

2021-09-09 09:28:08

面向列數(shù)據(jù)庫面向行

2016-04-13 11:31:32

數(shù)據(jù)分析數(shù)據(jù)獲取大數(shù)據(jù)應(yīng)用
點贊
收藏

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