Commit 5e50c4fb authored by Ramanayaka D.H.'s avatar Ramanayaka D.H.

Upload New File

parent 09fc641f
import dash
from dash import html
from dash import dcc
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
from app import app
import pymongo
import pandas as pd
import plotly.express as px
#establishes a connection to the MongoDB database hosted at the specified URL
client = pymongo.MongoClient("mongodb+srv://admin:admin@cluster0.afxihmt.mongodb.net/")
db = client["user_database"] #selects a specific database called "user_database" from the MongoDB server.
users_collection = db["users"]#selects a collection called "users" from the user_database
# user1 = user.User("","")
history_collection = db["history"] #selects a collection called "history" from the user_database
all_scorees = [] #initializes an empty list called "all_scores."
prof = html.Div([ #outermost container for the profile
html.Div([ #to display user profile history
html.Div(children=[],className="scroll-container",id="profile-history"),
html.Div([
dcc.Graph(id='line-chart') #contains a graph
],className="profile-graph",id="profile-graph"),
],className="profile-inner-box-1"),
html.Div([
html.Div([
html.H1("Profile"),
html.Div([
html.Div([ #users details updating with name, email, password, confirm password
html.Label("Name"),
dcc.Input(id="profile-name-input",type="text",className="form-control",placeholder="Enter name"),
],className="profile-inner-box-input"),
html.Div([
html.Label("Email"),
dcc.Input(id="profile-email-input",type="email",className="form-control",placeholder="Enter email",disabled=True),
],className="profile-inner-box-input"),
html.Div([
html.Label("Password"),
dcc.Input(id="profile-password-input", type="password",className="form-control",placeholder="Password")],className="profile-inner-box-input"),
html.Div([
html.Label("Confirm Password"),
dcc.Input(id="profile-confirm-password-input", type="password",className="form-control",placeholder="Password")],className="profile-inner-box-input"),
html.Center([dbc.Button("Update",className="btn btn-light", id="profile-button")]) , #update button
html.Div(id="profile-status",children=[])
])
],className="card profile-inner-box-input"), #image
html.Img(src=app.get_asset_url('user_profile.gif'),id="profile-vector"),
],className="profile-inner-box-2"),
],className="profile-outer-box")
# Define the layout of the profile page
@app.callback([Output('profile-history', 'children'),Output('line-chart', 'figure'),Output('profile-name-input', 'value'),Output('profile-email-input', 'value')],[Input('profile', 'n_clicks'),Input("intermediate-value", "data"),Input('line-chart', 'relayoutData')])
def profile_history(n_clicks,current_user,relayout_data):#function that executes when the input changes
query = {'username': current_user}#creating a query dictionary to find a user with the given current_user username.
try:
user = users_collection.find_one(query) #attempting to find a user document in the users_collection
if 'name' not in user.keys():
user['name'] = user['username'] #if name is not in the username, it adds it.
if not current_user == 'False' or current_user is not None: #A new query is created to find historical data
query = {'user': current_user} # Replace 'field_name' with your actual field name
data = history_collection.find(query) #retrieves historical data
card_elements = []
for entry in data: #a loop iterates over the historical data
all_scorees.append(entry['score'])
card = dbc.Card(
dbc.CardBody([
html.H4(entry['score'], className='card-title'),
html.P(entry['leg_ball'], className='card-text'),
html.P(entry['no_ball'], className='card-text'),
html.P(entry['danger_ball'], className='card-text'),
html.P(entry['arm-_ball'], className='card-text')
],className="single-card-body")
)
card_elements.append(card) #df is created as a Pandas DataFrame from the all_scores list.
df = pd.DataFrame(all_scorees, columns = ['score'])
print(df)
if relayout_data: #draws the graph
return card_elements,px.line(df, x=df.index, y='score', title='Overall History'),user['name'],user['username']
except Exception as e1:
print("exception history ", e1)
return card_elements,px.line(df, x=df.index, y='score', title='Overall History'),"",""
# print(current_user,"current_user profile")
# return [card_elements]
# @app.callback([Output('profile-name-input', 'value'),Output('profile-email-input', 'value')],[Input('profile', 'n_clicks'),Input("intermediate-value", "data")])
# def update_field(n_clicks,current_user):
# print(current_user,"current_user profile")
# query = {'username': current_user}
# try:
# user = users_collection.find_one(query)
# return user['name'],user['username']
# except:
# return "",""
@app.callback( #function does when the update button is clicked
Output("profile-status", "children"),
Input("profile-button", "n_clicks"),
State("profile-name-input", "value"),
State("profile-email-input", "value"),
State("profile-password-input", "value"),
State("profile-confirm-password-input", "value")
)
def update_profile(n_clicks, name, email, password, confirm_password): #update all these fields
if n_clicks:
if password != confirm_password:
return "Passwords do not match." #if both the pws don't match
else:
try: #sets the updated field values in the mongodb database.
users_collection.update_one({"username": email}, {"$set": {"name": name, "password": password}})
return "Profile updated successfully." #if the updated goes successfully.
except:
return "Profile failed to update." #if any error occurs
return ""
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment