import json
import dash
from dash import callback, Input, Output, State, callback_context, html, dcc
import requests
from dash.exceptions import PreventUpdate
app = dash.Dash()
def getting_data():
url = "http://127.0.0.1:8000"
end_point = "/api/user"
response = requests.get(url+end_point)
content = response.content.decode()
return content
data = json.loads(getting_data())
for i in range(len(data)):
@app.callback(
Output("container", 'children'),
Input(f"remove-btn-"+data[i]["emp_id"] , 'n_clicks')
)
def removeData(m,n,o):
data = json.loads(getting_data())
for i in range(len(data)):
if (m == None and n == None and o == None):
raise PreventUpdate
else:
emp_id = data[i]["emp_id"]
print(f"Employee id {i} "+emp_id)
url = "http://127.0.0.1:8000"
end_point = f"/api/user/{emp_id}"
responce = requests.delete(url+end_point)
print("response", responce)
url = "http://127.0.0.1:8000"
end_point = "/api/user"
response = requests.get(url+end_point)
content = response.content.decode()
data = json.loads(content)
return [html.Div(className="list", id="emp-list-"+data[i]["emp_id"], children=[
html.P(className="emp-list",
children=["Employee ID: "+data[i]["emp_id"]]),
html.P(className="emp-list",
children=["Employee First Name: "+data[i]["emp_first_name"]]),
html.P(className="emp-list",
children=["Employee Last Name: "+data[i]["emp_last_name"]]),
html.P(className="emp-list",
children=["Role: "+str(data[i]['roles'])]),
html.Div(className="btn-grp", children=[html.Button(children=["Edit"]),
html.Button(
id="remove-btn-"+data[i]["emp_id"], value=str(i),n_clicks=0, children=["Remove"])
]),
])for i in range(len(data))]
app.layout = html.Div(children=[html.Div(id="container", children=[
html.Div(className="list", id="emp-list-"+data[i]["emp_id"], children=[
html.P(className="emp-list",
children=["Employee ID: "+data[i]["emp_id"]]),
html.P(className="emp-list",
children=["Employee First Name: "+data[i]["emp_first_name"]]),
html.P(className="emp-list",
children=["Employee Last Name: "+data[i]["emp_last_name"]]),
html.P(className="emp-list",
children=["Role: "+str(data[i]['roles'])]),
html.Div(className="btn-grp", children=[html.Button(children=["Edit"]),
html.Button(
id=f"remove-btn-"+data[i]["emp_id"], children=["Remove"])
]),
html.Div(id=f"data{i}")
])for i in range(len(data))]),
dcc.Interval(id='interval-component', interval=1000, n_intervals=0),
])
if __name__ == ("__main__"):
app.run_server(debug=True)