Switch to using polars
This commit is contained in:
27
utils.py
27
utils.py
@@ -1,7 +1,7 @@
|
||||
import colorsys
|
||||
from datetime import datetime, date, timedelta
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
import plotly.express as px
|
||||
import plotly.io as pio # Keep for now, might remove later if generate_plot is fully replaced
|
||||
import math
|
||||
@@ -110,32 +110,7 @@ def get_distinct_colors(n):
|
||||
colors.append(hex_color)
|
||||
return colors
|
||||
|
||||
def generate_plot(df: pd.DataFrame, title: str) -> str:
|
||||
"""
|
||||
Analyzes the DataFrame and generates an appropriate Plotly visualization.
|
||||
Returns the Plotly figure as a div string.
|
||||
Optimized for speed.
|
||||
"""
|
||||
if df.empty:
|
||||
return "<p>No data available to plot.</p>"
|
||||
|
||||
num_columns = len(df.columns)
|
||||
|
||||
# Dictionary-based lookup for faster decision-making
|
||||
plot_funcs = {
|
||||
1: lambda: px.histogram(df, x=df.columns[0], title=title)
|
||||
if pd.api.types.is_numeric_dtype(df.iloc[:, 0]) else px.bar(df, x=df.columns[0], title=title),
|
||||
|
||||
2: lambda: px.scatter(df, x=df.columns[0], y=df.columns[1], title=title)
|
||||
if pd.api.types.is_numeric_dtype(df.iloc[:, 0]) and pd.api.types.is_numeric_dtype(df.iloc[:, 1])
|
||||
else px.bar(df, x=df.columns[0], y=df.columns[1], title=title)
|
||||
}
|
||||
|
||||
# Select plot function based on column count
|
||||
fig = plot_funcs.get(num_columns, lambda: px.imshow(df.corr(numeric_only=True), text_auto=True, title=title))()
|
||||
|
||||
# Use static rendering for speed
|
||||
return pio.to_html(fig, full_html=False, include_plotlyjs=False, config={'staticPlot': True})
|
||||
|
||||
|
||||
def calculate_estimated_1rm(weight, repetitions):
|
||||
|
||||
Reference in New Issue
Block a user