Plotting charts with python class: AttributeError: 'NoneType' object has no attribute 'generate_plot'

Viewed 78

I tried to generate charts with the following python class but it throws back an AttributeError. Can someone please help me fix this?

from typing import Dict, Any
import streamlit as stl
import pandas as pd

class Chart:
    def __init__(self, dataframe: pd.DataFrame, config_dict: Dict[str, Any]):
        self._dataframe = dataframe
        self._config_dict = config_dict

    @abstractmethod
    def generate_plot(self) -> stl.plotly_chart:
        NotImplementedError()


def OpenCloseChart(Chart):
    def generate_plot(self) -> stl.plotly_chart:
        openCloseChart_fig = self._dataframe.iplot(
            y=self._config_dict['y_data'],
            secondary_y=self._config_dict['secondary_y'],
            secondary_y_title=self._config_dict['secondary_y_title'],
            xTitle=self._config_dict['xTitle'],
            yTitle=self._config_dict['yTitle'],
            title=self._config_dict['title'],
            asFigure=self._config_dict['asFigure'])
        return stl.plotly_chart(openCloseChart_fig)

I call on the class in the function below but it throws back AttributeError

def create_open_close_chart(ticker_df: pd.DataFrame, name: str,
                                    start_date: str,
                                    end_date: str) -> stl.plotly_chart:
            config_dict = {
                'y_data': ticker_df.Open,
                "secondary_y": 'Close Price ($)',
                'secondary_y_title': "Close Price ($)",
                "xTitle": "Date",
                "yTitle": "Open Price ($)",
                "title":
                f"Open and Close Price of {name} From {start_date} To {end_date}",
                "asFigure": True
            }
            open_close_chart_obj = OpenCloseChart(Chart(ticker_df, config_dict))
            return open_close_chart_obj.generate_plot()

        create_open_close_chart(df, company_name, df_actual_start_date, end_date)

Error: enter image description here

I expect an output as the fig below. enter image description here

1 Answers

I am guessing you were trying to create an object, not a function, and use class OpenCloseChart(Chart): instead of def OpenCloseChart(Chart):

Related