Py.Cafe

nilolom565/

rcgrid

DocsPricing
  • assets/
  • app.py
  • requirements.txt
app.py
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
from dash import Dash, html, Input, Output
import dash_ag_grid as dag
import pandas as pd

url = "https://www.ratingscentral.com/MatchList.php?CSV_Output=Text&PlayerID=142510"

df = pd.read_csv(url)

# 0EventID,EventDate,OpponentID,OpponentName,OpponentMean,
# 5OpponentStDev,WonLost,7PointChange,8MatchesPlayersPlayedInEvent,Score,
# PlayerMean,11PlayerStDev
# Drop columns by index (column positions start from 0)
df = df.drop(df.columns[[0, 5, 7, 8, 11]], axis=1)

df = df.sort_values(by=df.columns[0], ascending=False)

history_wl = (
    df.groupby(df.columns[1])[df.columns[4]].value_counts().unstack(fill_value=0)
)
history_wl["W/L"] = history_wl.apply(
    lambda row: f"{row.get('W', 0)} / {row.get('L', 0)}", axis=1
)
df = df.merge(history_wl[["W/L"]], left_on=df.columns[1], right_index=True)

######################################
url = "https://www.ratingscentral.com/PlayerList.php?PlayerID=142510&PlayerSport=Any&SortOrder=Name&CSV_Output=Text"
playerdf = pd.read_csv(url)

# Extract player details from row index 0 (first row)
player_info = playerdf.iloc[0]

# Extract relevant values
rating = player_info["Rating"]
stdev = player_info["StDev"]
last_played = player_info["LastPlayed"]
last_event = player_info["LastEvent"]
######################################

app = Dash(__name__)

grid = dag.AgGrid(
    id="quickstart-grid",
    rowData=df.to_dict("records"),

    columnDefs = [
        {"field": col, "hide": i in [1,6]}
        for i, col in enumerate(df.columns)
    ],

    defaultColDef={
        "resizable": True,
        "sortable": True,
        "filter": True,
        #"minWidth": 125,
    },
    columnSize="sizeToFit",
    dashGridOptions={
        "rowSelection": "single",  # Enable row selection
    },
)

app.layout = html.Div(
    [
        html.P(
            [
                f"{rating} ± {stdev} ",
                html.A(
                    last_played,
                    href=f"https://www.ratingscentral.com/EventDetail.php?EventID={last_event}#P142510",
                    target="_blank",
                ),
            ]
        ),
        grid,
        html.Div(id="quickstart-output"),
    ]
)

from dash import Input, Output, State

@app.callback(
    Output("quickstart-output", "children"),
    Input("quickstart-grid", "selectedRows"),
)
def display_selected_row(selected):
    if not selected:
        return "Click a row to see details."

    row_data = selected[0]  # Only one row is selected
    player_id = row_data.get("OpponentID")

    if not player_id:
        return "OpponentID missing from selected row."

    matching_rows = df[df["OpponentID"] == player_id]

    link = html.A(
        row_data.get("OpponentName", "Link"),
        href=f"https://www.ratingscentral.com/PlayerHistory.php?PlayerID={player_id}",
        target="_blank",
    )

    return html.Div([
        html.Pre(matching_rows.to_string(index=False)),
        link,
    ])



if __name__ == "__main__":
    app.run_server(debug=True)