Skip to content

Commit

Permalink
[PdrDashboard] Use polars syntax for dataframes in pdr dashboard (#1619)
Browse files Browse the repository at this point in the history
  • Loading branch information
calina-c authored Sep 24, 2024
1 parent e1a8eeb commit 3507ece
Show file tree
Hide file tree
Showing 15 changed files with 339 additions and 249 deletions.
10 changes: 4 additions & 6 deletions pdr_backend/pdr_dashboard/callbacks/callbacks_feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def update_page_data(_start_date):
for key, value in app.data.feeds_metrics_data.items()
]

return app.data.feeds_table_data.to_dict("records"), metrics_children_data
return app.data.feeds_table_data.to_dicts(), metrics_children_data

@app.callback(
Output("feeds_page_table", "data"),
Expand Down Expand Up @@ -97,12 +97,10 @@ def filter_table(

if sort_by:
sort_col = sort_by[0]["column_id"]
ascending = sort_by[0]["direction"] == "asc"
new_table_data = new_table_data.sort_values(
by=sort_col, ascending=ascending
)
descending = sort_by[0]["direction"] == "desc"
new_table_data = new_table_data.sort(by=sort_col, descending=descending)

return format_df(new_table_data).to_dict("records")
return format_df(new_table_data).to_dicts()

@app.callback(
Output("sales_dropdown", "label"),
Expand Down
34 changes: 17 additions & 17 deletions pdr_backend/pdr_dashboard/callbacks/callbacks_home.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dash
import pandas
import polars as pl
from dash import Input, Output, State

from pdr_backend.cli.arg_feeds import ArgFeeds
Expand Down Expand Up @@ -53,7 +53,7 @@ def get_display_data_from_db(
]

if len(selected_feeds) == 0 or len(selected_predictoors_addrs) == 0:
payouts = pandas.DataFrame()
payouts = pl.DataFrame()
else:
payouts = app.data.payouts_from_bronze_predictions(
selected_feeds_addrs,
Expand Down Expand Up @@ -113,18 +113,18 @@ def update_predictoors_table_on_search(
app.data.refresh_feeds_data()

formatted_predictoors_data = (
app.data.formatted_predictoors_home_page_table_data.copy()
app.data.formatted_predictoors_home_page_table_data.clone()
)
selected_predictoors_addrs = [
predictoors_table[i]["full_addr"] for i in selected_rows
]

if "show-favourite-addresses.value" in dash.callback_context.triggered_prop_ids:
custom_predictoors = formatted_predictoors_data[
formatted_predictoors_data["full_addr"].isin(
custom_predictoors = formatted_predictoors_data.filter(
formatted_predictoors_data["full_addr"].is_in(
app.data.favourite_addresses
)
]
)
custom_predictoors_addrs = list(custom_predictoors["full_addr"])

if show_favourite_addresses:
Expand All @@ -136,23 +136,23 @@ def update_predictoors_table_on_search(
if predictoor_addr not in custom_predictoors_addrs
]

filtered_data = formatted_predictoors_data.copy()
filtered_data = formatted_predictoors_data.clone()
if search_value:
filtered_data = filtered_data[
filtered_data = filtered_data.filter(
filtered_data["full_addr"].str.contains(search_value)
]
)

filtered_data = filtered_data[
~filtered_data["full_addr"].isin(selected_predictoors_addrs)
]
selected_predictoors = formatted_predictoors_data[
formatted_predictoors_data["full_addr"].isin(selected_predictoors_addrs)
]
filtered_data = filtered_data.filter(
~filtered_data["full_addr"].is_in(selected_predictoors_addrs)
)
selected_predictoors = formatted_predictoors_data.filter(
formatted_predictoors_data["full_addr"].is_in(selected_predictoors_addrs)
)

filtered_data = pandas.concat([selected_predictoors, filtered_data])
filtered_data = pl.concat([selected_predictoors, filtered_data])
selected_predictoor_indices = list(range(len(selected_predictoors_addrs)))

return (filtered_data.to_dict("records"), selected_predictoor_indices)
return (filtered_data.to_dicts(), selected_predictoor_indices)

@app.callback(
Output("feeds_table", "data"),
Expand Down
10 changes: 5 additions & 5 deletions pdr_backend/pdr_dashboard/callbacks/callbacks_predictoors.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def update_page_data(_start_date):
for key, value in stats.items()
]

return app.data.predictoors_table_data.to_dict("records"), metrics_children_data
return app.data.predictoors_table_data.to_dicts(), metrics_children_data

@app.callback(
Output("predictoors_page_table", "data"),
Expand Down Expand Up @@ -115,12 +115,12 @@ def filter_table(
if sort_by:
# Extract sort criteria
sort_col = sort_by[0]["column_id"]
ascending = sort_by[0]["direction"] == "asc"
new_table_data = new_table_data.sort_values(
by=[sort_col, "full_addr"], ascending=ascending
descending = sort_by[0]["direction"] == "desc"
new_table_data = new_table_data.sort(
by=[sort_col, "full_addr"], descending=descending
)

return format_df(new_table_data).to_dict("records")
return format_df(new_table_data).to_dicts()

@app.callback(
Output("apr_dropdown", "label"),
Expand Down
6 changes: 3 additions & 3 deletions pdr_backend/pdr_dashboard/dash_components/modal.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List, Optional

import dash_bootstrap_components as dbc
import pandas
import polars as pl
from dash import html

from pdr_backend.cli.arg_feeds import ArgFeed
Expand Down Expand Up @@ -85,9 +85,9 @@ def create_figures(self):

if not selected_row:
figures_args: List[List] = (
[pandas.DataFrame(), pandas.DataFrame()]
[pl.DataFrame(), pl.DataFrame()]
if self.modal_id == "feeds_modal"
else [pandas.DataFrame()]
else [pl.DataFrame()]
)
self.figures = figures_func(*figures_args).get_figures()
return
Expand Down
Loading

0 comments on commit 3507ece

Please sign in to comment.