73 lines
2.2 KiB
Python
73 lines
2.2 KiB
Python
import os
|
|
import pandas as pd
|
|
import plotly.graph_objects as go
|
|
from abc import ABC, abstractmethod
|
|
|
|
from .base import BaseAnalysis
|
|
from app.analysis.data_utils import prepare_data, mk_plotdir
|
|
|
|
# -------------------------------------------
|
|
# Base Class for All Plotly Plot Analyses
|
|
# -------------------------------------------
|
|
class BasePlotlyAnalysis(BaseAnalysis, ABC):
|
|
"""
|
|
Base class for all Plotly plot-based analyses.
|
|
It enforces a structure for:
|
|
- Data preparation
|
|
- Transformation
|
|
- Plot generation
|
|
- Memory cleanup
|
|
|
|
Attributes:
|
|
plot_filename (str): The filename for the output plot.
|
|
alt_text (str): The alt text for the plot.
|
|
"""
|
|
plot_filename = "default_plot.html"
|
|
alt_text = "Default Alt Text"
|
|
|
|
def execute(self, df: pd.DataFrame):
|
|
"""
|
|
Executes the full analysis pipeline.
|
|
|
|
Parameters:
|
|
df (pd.DataFrame): The input DataFrame containing user activity data.
|
|
|
|
Returns:
|
|
str: HTML iframe containing the URL to the generated plot.
|
|
"""
|
|
df = prepare_data(df) # Step 1: Prepare data
|
|
|
|
paths = mk_plotdir(self.plot_filename)
|
|
self.output_path, self.plot_url = paths['output_path'], paths['plot_url']
|
|
|
|
df = self.transform_data(df) # Step 2: Transform data (implemented by subclass)
|
|
self.plot_data(df) # Step 3: Create the plot
|
|
|
|
# Save the plot as an HTML file
|
|
self.fig.write_html(self.output_path)
|
|
|
|
del df # Step 4: Free memory
|
|
return f'<iframe src="{self.plot_url}" width="100%" height="600"></iframe>'
|
|
|
|
@abstractmethod
|
|
def transform_data(self, df: pd.DataFrame) -> pd.DataFrame:
|
|
"""
|
|
Subclasses must define how they transform the data.
|
|
|
|
Parameters:
|
|
df (pd.DataFrame): The input DataFrame containing user activity data.
|
|
|
|
Returns:
|
|
pd.DataFrame: The transformed DataFrame.
|
|
"""
|
|
pass
|
|
|
|
@abstractmethod
|
|
def plot_data(self, df: pd.DataFrame):
|
|
"""
|
|
Subclasses must define how they generate the plot.
|
|
|
|
Parameters:
|
|
df (pd.DataFrame): The transformed DataFrame containing data to be plotted.
|
|
"""
|
|
pass |