And this is my 3rd page:
pg3.py
from dash.dependencies import Input, Output, State
import warnings
import dash
import dash_bootstrap_components as dbc
import pandas as pd
from dash import dash_table
from dash import dcc, callback
from dash import html
from dash.dependencies import Input, Output, State
from dash_labs.plugins import register_page
from app import app
from app import db
warnings.simplefilter("ignore")
# To create meta tag for each page, define the title, image, and description.
register_page(__name__,
path='/pg3', # '/' is home page and it represents the url
name='Network Analysis', # name of page, commonly used as name of link
title='Network', # title that appears on browser's tab
# image='pg1.png', # image in the assets folder
# description='Histograms are the new bar charts.'
)
layout = dbc.Container([
dbc.Row([dbc.Col([
html.Div(children=[
dbc.ButtonGroup([
dbc.Button("Table", id="table", n_clicks=0), dbc.Button("Save", id="save-table", n_clicks=0)
])
])
], width= 1),
dbc.Col([ html.Div(id='placeholder', children=[]),
dcc.Interval(id='interval', interval=1000)], width = 0 )]),
dbc.Row([dbc.Col([ dash_table.DataTable(id="datatable_id", data=[{'Product': 'Iphone', 'Version': '6a', 'Price': 799, 'Sales': 2813},
{'Product': 'Iphone', 'Version': '9', 'Price': 900, 'Sales': 5401},
{'Product': 'Iphone', 'Version': '7', 'Price': 799, 'Sales': 2513},
{'Product': 'Iphone', 'Version': '8', 'Price': 850, 'Sales': 5401},
{'Product': 'Galaxy', 'Version': 'S9', 'Price': 900, 'Sales': 6084},
{'Product': 'Galaxy', 'Version': 'S10', 'Price': 1000, 'Sales': 7084},
{'Product': 'Galaxy', 'Version': 'S20', 'Price': 1200, 'Sales': 9084},
{'Product': 'Pixel', 'Version': '1', 'Price': 400, 'Sales': 2084},
{'Product': 'Pixel', 'Version': '2', 'Price': 500, 'Sales': 3033},
{'Product': 'Pixel', 'Version': '3', 'Price': 600, 'Sales': 6000}], columns=[{'name': 'Product', 'id': 'Product', 'deletable': False, 'renamable': False},
{'name': 'Version', 'id': 'Version', 'deletable': True, 'renamable': True},
{'name': 'Price', 'id': 'Price', 'deletable': True, 'renamable': True},
{'name': 'Sales', 'id': 'Sales', 'deletable': False, 'renamable': False}], filter_action="native",
sort_action="native",
sort_mode="multi",
row_selectable="multi",
editable=True,
row_deletable=False,
selected_rows=[],
page_action="native",
page_size=10,
# fixed_columns={ 'headers': True, 'data': 0 },
style_cell={'textAlign': 'center', "font-size": 12},
style_header={'height': 40,'backgroundColor': '#2338a1','color': 'white', "font-size": 14},
style_table={'overflowX': 'auto'}
) ])])
], fluid = True)
# @callback([
# Output('datatable_id', 'data'),
# Output('datatable_id', 'columns')],
#
# [
# Input('store-data-frame', 'data'),
# Input('table', 'n_clicks')],
# prevent_initial_call=True)
# def table(data, n_clicks):
# print(n_clicks)
# if n_clicks == 1:
# for k, v in data.items():
# if "df" in k:
# dff = pd.DataFrame.from_dict(v)
# data = dff.to_dict("records")
# columns = [{'name': i, 'id': i, "hideable": True} for i in dff.columns]
# return data, columns
# elif n_clicks > 1:
# dff = pd.read_sql_table('Your_Sales_Data', con=db.engine)
# data = dff.to_dict("records")
# columns = [{'name': i, 'id': i, "hideable": True} for i in dff.columns]
# return data, columns
@callback(
[Output('placeholder', 'children'),
Output("store-interval", "data")],
[Input('save-table', 'n_clicks'),
Input("interval", "n_intervals")],
[State('datatable_id', 'data'),
State('store-interval', 'data')],
prevent_initial_call=True
)
def df_to_csv(n_clicks, n_intervals, dataset, s):
output = html.Plaintext("The data has been saved to your database.",
style={'color': 'black', 'font-weight': 'bold', 'font-size': 'Large','margin': "5px"})
no_output = html.Plaintext("", style={'margin': "0px"})
input_triggered = dash.callback_context.triggered[0]["prop_id"].split(".")[0]
if input_triggered == "save-table" and n_clicks > 0:
s = 2
df = pd.DataFrame(dataset)
df.to_sql("Your_Sales_Data", con=db.engine, if_exists='replace', index=False)
return output, s
elif input_triggered == 'interval' and s > 0:
s = s-1
if s > 0:
return output, s
else:
return no_output, s
elif s == 0:
return no_output, s