Hi Team,
Issue 1 - I created a dashboard and gave pagination fields but my output is not showing the pagination fields in the o/p. Though I have 150+ rows in the data, it is not showing pagination.
code is also attached.
Code Help - How to use dcc.Interval to call my getData() method only after every 2 hours?
import dash
import dash_table
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
from dash.dependencies import State, Input, Output
import pandas as pd
from bs4 import BeautifulSoup
import requests
import re
import numpy as np
def getdata():
url="https://www.worldometers.info/coronavirus/"
# Make a GET request to fetch the raw HTML content
html_content = requests.get(url).text
# Parse the html content
soup = BeautifulSoup(html_content, "lxml")
strlastdate=soup.find('div',text=re.compile("Last updated")).text
gdp_table = soup.find("table", id = "main_table_countries_today")
gdp_table_data = gdp_table.tbody.find_all("tr")
# Getting all countries names
dicts = {}
for i in range(len(gdp_table_data)):
try:
key = (gdp_table_data[i].find_all('a', href=True)[0].string)
except:
key = (gdp_table_data[i].find_all('td')[0].string)
value = [j.string for j in gdp_table_data[i].find_all('td')]
dicts[key] = value
live_data= pd.DataFrame(dicts).drop(0).T.iloc[:,:7]
live_data.columns = ["Total Cases","New Cases", "Total Deaths", "New Deaths", "Total Recovered","Active","Serious Critical"]
live_data.index.name = 'Country'
live_data.iloc[:,:7].to_csv("input_data/base_data1.csv")
df = pd.read_csv('input_data/base_data1.csv')
df = df.replace(r'^\s*$', np.NaN, regex=True)
col = ['Total Cases', 'New Cases', 'Total Deaths', 'New Deaths',
'Total Recovered', 'Active', 'Serious Critical']
for i in col:
df[i] = df[i].str.replace(',', '')
df[i] = df[i].fillna(0)
df[i]=df[i].astype('int64')
df.sort_values('Total Cases', ascending=False, inplace=True)
df.sort_values('Total Cases', ascending=False, inplace=True)
return(df,strlastdate)
app = dash.Dash()
df,strlastdate=getdata()
app.layout= html.Div([
html.H1('Corona Virus Dashboard - Latest Updates!'),
html.H3(strlastdate),
html.Div(style={'padding': 10}),
html.Div([dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i, "selectable": True} for i in df.columns],
data=df.to_dict('records'),
filter_action="native",
sort_action="native",
sort_mode="single",
row_selectable="multi",
selected_rows=[],
page_action="native",
page_current= 0,
page_size= 15,
#style_as_list_view=True,
style_cell={'padding': '5px','backgroundColor': 'white',
'color': 'black','border': '1px solid black' },
style_header={
'backgroundColor': 'rgb(50, 50, 50)',
'color': 'white',
'fontWeight': 'bold',
'border': '1px solid black'
},
style_data={ 'border': '1px solid black' },
style_data_conditional=[
{
'if': {
'column_id': 'Total Deaths',
'filter_query': '{Total Deaths} > 10000'
},
'backgroundColor': '#FF0000',
'color': 'yellow',
}
]
)]),html.Div(id='datatable-interactivity-container'),
dcc.Markdown('''
This is the [Data Source](https://www.worldometers.info/coronavirus/)
''')
])
@app.callback(
Output('datatable-interactivity-container', "children"),
[Input('table', "derived_virtual_data"),
Input('table', "derived_virtual_selected_rows")])
def update_graphs(rows, derived_virtual_selected_rows):
if derived_virtual_selected_rows is None:
derived_virtual_selected_rows = []
dff = df if rows is None else pd.DataFrame(rows)
colors = ['#FFBF00' if i in derived_virtual_selected_rows else '#FF0000'
for i in range(len(dff))]
return [
dcc.Graph(
id=column,
figure={
"data": [
{
"x": dff["Country"],
"y": dff[column],
"type": "bar",
"marker": {"color": colors},
}
],
"layout": {
"xaxis": {"automargin": True},
"yaxis": {
"automargin": True,
"title": {"text": column}
},
"height": 250,
"margin": {"t": 10, "l": 10, "r": 10},
},
},
)
# check if column exists - user may have deleted it
# If `column.deletable=False`, then you don't
# need to do this check.
for column in ["Total Cases", "Total Deaths", "New Cases"] #if column in dff
]
if __name__ == '__main__':
app.run_server()