1.啟動EduBlocks網站,選擇下方Start Creating按鈕。
2.選擇Python程式按鈕。
5.選擇Graph範例
6.查看樍木式程式結構
7.執行結果
8.查看程式
「智慧生活科技專業社群」由 Nantou.py 使用者社群與國立虎尾科技大學電機資訊學院共同維護,匯集關注Python、人工智慧、物聯網與智慧生活應用的教師、學生及實務工作者。本社群以「科技融入生活、教學連結場域、知識促進共好」為核心,分享Python程式設計、AI協作學習、AIoT、網際網路與雲端服務、智慧農業、福祉科技、永續發展及USR社會實踐等內容,並將技術導入課堂、社區與真實生活場域。部分文章運用生成式AI協助資料整理、內容架構、程式範例與文字潤飾,文章主題、場域經驗、內容查核及最終修訂則由作者與社群成員負責。我們期待透過人與AI協作,讓科技成為改善生活、連結世代、支持地方與實踐永續的力量。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | import dash import dash_core_components as dcc import dash_html_components as html import plotly.graph_objects as go from dash.dependencies import Input, Output import pandas as pd baseURL = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/" external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] tickFont = {'size':12, 'color':"rgb(30,30,30)", 'family':"Courier New, monospace"} def loadData(fileName, columnName): data = pd.read_csv(baseURL + fileName) \ .drop(['Lat', 'Long'], axis=1) \ .melt(id_vars=['Province/State', 'Country/Region'], var_name='date', value_name=columnName) \ .fillna('<all>') data['date'] = data['date'].astype('datetime64[ns]') return data allData = loadData("time_series_19-covid-Confirmed.csv", "CumConfirmed") \ .merge(loadData("time_series_19-covid-Deaths.csv", "CumDeaths")) \ .merge(loadData("time_series_19-covid-Recovered.csv", "CumRecovered")) countries = allData['Country/Region'].unique() countries.sort() app = dash.Dash(__name__, external_stylesheets=external_stylesheets) app.layout = html.Div( style={ 'font-family':"Courier New, monospace" }, children=[ html.H1('Case History of the Coronavirus (COVID-19)'), html.Div(className="row", children=[ html.Div(className="four columns", children=[ html.H5('Country'), dcc.Dropdown( id='country', options=[{'label':c, 'value':c} for c in countries], value='Italy' ) ]), html.Div(className="four columns", children=[ html.H5('State / Province'), dcc.Dropdown( id='state' ) ]), html.Div(className="four columns", children=[ html.H5('Selected Metrics'), dcc.Checklist( id='metrics', options=[{'label':m, 'value':m} for m in ['Confirmed', 'Deaths', 'Recovered']], value=['Confirmed', 'Deaths'] ) ]) ]), dcc.Graph( id="plot_new_metrics", config={ 'displayModeBar': False } ), dcc.Graph( id="plot_cum_metrics", config={ 'displayModeBar': False } ) ]) @app.callback( [Output('state', 'options'), Output('state', 'value')], [Input('country', 'value')] ) def update_states(country): states = list(allData.loc[allData['Country/Region'] == country]['Province/State'].unique()) states.insert(0, '<all>') states.sort() state_options = [{'label':s, 'value':s} for s in states] state_value = state_options[0]['value'] return state_options, state_value def nonreactive_data(country, state): data = allData.loc[allData['Country/Region'] == country] if state == '<all>': data = data.drop('Province/State', axis=1).groupby("date").sum().reset_index() else: data = data.loc[data['Province/State'] == state] newCases = data.select_dtypes(include='int64').diff().fillna(0) newCases.columns = [column.replace('Cum', 'New') for column in newCases.columns] data = data.join(newCases) data['dateStr'] = data['date'].dt.strftime('%b %d, %Y') return data def barchart(data, metrics, prefix="", yaxisTitle=""): figure = go.Figure(data=[ go.Bar( name=metric, x=data.date, y=data[prefix + metric], marker_line_color='rgb(0,0,0)', marker_line_width=1, marker_color={ 'Deaths':'rgb(200,30,30)', 'Recovered':'rgb(30,200,30)', 'Confirmed':'rgb(100,140,240)'}[metric] ) for metric in metrics ]) figure.update_layout( barmode='group', legend=dict(x=.05, y=0.95, font={'size':15}, bgcolor='rgba(240,240,240,0.5)'), plot_bgcolor='#FFFFFF', font=tickFont) \ .update_xaxes( title="", tickangle=-90, type='category', showgrid=True, gridcolor='#DDDDDD', tickfont=tickFont, ticktext=data.dateStr, tickvals=data.date) \ .update_yaxes( title=yaxisTitle, showgrid=True, gridcolor='#DDDDDD') return figure @app.callback( Output('plot_new_metrics', 'figure'), [Input('country', 'value'), Input('state', 'value'), Input('metrics', 'value')] ) def update_plot_new_metrics(country, state, metrics): data = nonreactive_data(country, state) return barchart(data, metrics, prefix="New", yaxisTitle="New Cases per Day") @app.callback( Output('plot_cum_metrics', 'figure'), [Input('country', 'value'), Input('state', 'value'), Input('metrics', 'value')] ) def update_plot_cum_metrics(country, state, metrics): data = nonreactive_data(country, state) return barchart(data, metrics, prefix="Cum", yaxisTitle="Cumulated Cases") if __name__ == '__main__': app.run_server(debug=True) |
1 2 | import requests print(requests.get('https://coronavirus-tracker-api.herokuapp.com/v2/latest').text) |
1 2 | import requests print(requests.get('https://coronavirus-tracker-api.herokuapp.com/v2/locations').text) |
1 2 | import requests print(requests.get('https://coronavirus-tracker-api.herokuapp.com/v2/locations?country_code=US').text) |