From 54302b38d8d6be7e9c3d16ec0ddfcaea31d61109 Mon Sep 17 00:00:00 2001 From: Ayxan Date: Fri, 3 Jun 2022 01:35:14 +0400 Subject: [PATCH] Added run.py; main: added radio button --- app/__init__.py | 0 app/app.py | 12 ++++++++++++ app/main.py | 41 ++++++++++++++++++++++++++++------------- app/run.py | 4 ++++ 4 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 app/__init__.py create mode 100644 app/run.py diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/app.py b/app/app.py index f739a5b..4a2d707 100644 --- a/app/app.py +++ b/app/app.py @@ -1,3 +1,4 @@ +from matplotlib.pyplot import bar import pandas as pd import plotly.express as px from collections import Counter @@ -26,11 +27,22 @@ class App(): def createPieChart(self, data: pd.DataFrame, genres_type: str, selected_genres: List[str]): genres_count = self.getDataGenresCount(data, genres_type) + if len(selected_genres) < 2: return None df = pd.DataFrame({'title': genres_count.keys(), 'count': genres_count.values()}).\ query('title == @selected_genres') pie_chart = px.pie(df, names='title', values='count', title=genres_type + ' genres') return pie_chart + def createBarChart(self, data: pd.DataFrame, genres_type: str, selected_genres: List[str]): + genres_count = self.getDataGenresCount(data, genres_type) + if len(selected_genres) < 2: return None + genres_count_selected = {} + for i, j in genres_count.items(): + if i in selected_genres: + genres_count_selected[i] = j + bar_chart = px.bar(x=genres_count_selected.keys(), y=genres_count_selected.values()) + return bar_chart + def preprocessingData(self, upload_file: str) -> Optional[pd.DataFrame]: if upload_file is not None: data = pd.read_csv(upload_file) diff --git a/app/main.py b/app/main.py index f0cae81..a1b4336 100644 --- a/app/main.py +++ b/app/main.py @@ -6,27 +6,42 @@ class Main(): def __init__(self): self.app = App(page_title='Dashboard', page_icon=':bar_chart:', layout='wide') data = self.uploadFile() - selected_genres = self.sideBar(data) - if data is not None: - self.app.st.plotly_chart(self.app.createPieChart(data, 'All', selected_genres)) - # self.app.st.plotly_chart(self.app.createPieChart(data, 'Movie', selected_genres)) - # self.app.st.plotly_chart(self.app.createPieChart(data, 'Series')) + self.sideBar(data) + + self.addStyle() def sideBar(self, data: pd.DataFrame): - data = self.app.getDataGenresCount(data, 'all') - self.app.st.sidebar.header('Data by Genre:') + data_genres_count = self.app.getDataGenresCount(data, 'all') + genres_radio_btn = self.app.st.sidebar.radio('', ['All', 'Movie', 'Series']) + graphics_radio_btn = self.app.st.sidebar.radio('', ['Pie Chart', 'Bar Chart']) + # radio buttons vertical to horizontal + # self.app.st.write('', unsafe_allow_html=True) + # self.app.st.sidebar.header('Data by Genre:') data_genre_filter = self.app.st.sidebar.multiselect( 'Select Genre:', - options = data.keys(), - default = data.keys() + options = data_genres_count.keys(), + default = data_genres_count.keys() ) - return data_genre_filter + if data is not None: + try: + if graphics_radio_btn == 'Pie Chart': + self.app.st.plotly_chart(self.app.createPieChart(data, genres_radio_btn, data_genre_filter)) + else: + self.app.st.plotly_chart(self.app.createBarChart(data, genres_radio_btn, data_genre_filter)) + except: pass def uploadFile(self) -> pd.DataFrame: uploaded_file = 'app\\WATCHLIST.csv' # app.st.file_uploader("Choose a file") data = self.app.preprocessingData(uploaded_file) return data - - -if __name__ == '__main__': Main() \ No newline at end of file + + def addStyle(self): + hide_st_style = """ + + """ + self.app.st.markdown(hide_st_style, unsafe_allow_html=True) \ No newline at end of file diff --git a/app/run.py b/app/run.py new file mode 100644 index 0000000..be815d6 --- /dev/null +++ b/app/run.py @@ -0,0 +1,4 @@ +from main import Main + + +if __name__ == '__main__': Main() \ No newline at end of file