File size: 7,857 Bytes
cb22296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abb5fd4
cb22296
 
 
 
 
 
abb5fd4
cb22296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abb5fd4
cb22296
 
 
 
 
 
 
abb5fd4
cb22296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abb5fd4
cb22296
 
 
 
 
 
 
 
 
 
 
 
 
 
abb5fd4
cb22296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abb5fd4
cb22296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import plotly.graph_objects as go
import plotly.express as px
import streamlit as st
import pandas as pd
import numpy as np
from src.arcs import generate_arc
from src.preprocess import get_last_known_bounty, get_latest_age, get_main_crew
from configparser import ConfigParser, ExtendedInterpolation
import warnings
warnings.filterwarnings("ignore")

# all_dims = ['Chapter', 'Appearance', 'Arc', 'Character', 'Appearance Notes']

pl_config = ConfigParser(interpolation=ExtendedInterpolation())
pl_config.read('cfg/cfg.ini')

end_chap = pl_config['SCRAPER'].getint('end_chap') + 1
char_link_fp = pl_config['SCRAPER'].get('char_link_fp')
chap_appearance_fp = pl_config['SCRAPER'].get('chap_appearance_fp')
char_details_fp = pl_config['SCRAPER'].get('char_details_fp')
age_bounty_fp = pl_config['SCRAPER'].get('age_bounty_fp')

st.set_page_config(page_title='One Dash', layout = 'wide', initial_sidebar_state = 'auto')

@st.cache_data()
def generate_df():
    appearance_df = pd.read_csv(chap_appearance_fp)
    char_details_df = pd.read_csv(char_details_fp)
    df_age_bounty = pd.read_csv(age_bounty_fp)
    return appearance_df, char_details_df, df_age_bounty

@st.cache_data()
def fig_app_by_arc(appearance_df, height):
    fig_app_by_arc = px.histogram(appearance_df[appearance_df['Appearance'].isin(appearance_df['Appearance'].value_counts().head(20).index.tolist())], 
                                x='Appearance', 
                                color = 'Arc', 
                                barmode='group',
                                labels={
                                    "Appearance": "Name",
                                    "counts": "Counts"
                                },
                                height = height
                                )

    fig_app_by_arc.update_layout(
                                xaxis_title="Name",
                                yaxis_title="",
                                )
    return fig_app_by_arc

@st.cache_data()
def fig_app_by_arc_sunburst(appearance_df):
    fig_app_by_arc_sunburst = px.sunburst(appearance_df[appearance_df['Appearance'].isin(appearance_df['Appearance'].value_counts().head(10).index.tolist())], 
                                path = ['Appearance', 'Arc'],
                                width = 800,
                                height = 800)
    return fig_app_by_arc_sunburst

@st.cache_data()
def fig_latest_bounty(char_details_df, height):
    df = char_details_df[char_details_df['last_bounty'] > 0]
    df = df.sort_values(by = "last_bounty", ascending = False)
    fig_latest_bounty = px.bar(df.head(50), 
                            x = 'Name', 
                            y = 'last_bounty', 
                            width = 1000,
                            height = height, 
                            log_y = True)
    fig_latest_bounty.update_layout(
                                xaxis_title="Name",
                                yaxis_title="Last Bounty",
                                xaxis={'categoryorder':'total descending'}
                                )
    return fig_latest_bounty

@st.cache_data()
def fig_latest_bounty_dist(char_details_df, height):
    group_df = char_details_df[['main_crew','last_bounty']]
    group_df = group_df.groupby(['main_crew']).sum()
    group_df = group_df.sort_values(by= 'last_bounty', ascending=False)
    fig_latest_bounty_dist = px.bar(group_df.head(20), 
                                    x="last_bounty", 
                                    height = height)

    fig_latest_bounty_dist.update_layout(
                                xaxis_title="Bounty Group",
                                yaxis_title="",
                                )
    return fig_latest_bounty_dist

@st.cache_data()
def fig_latest_age_to_bounty(df_age_bounty,height):
    fig_latest_age_to_bounty = px.scatter(x = df_age_bounty['latest_age'], 
                                        y=df_age_bounty['last_bounty'], 
                                        color = df_age_bounty['Name'],
                                        labels={
                                            "latest_age": "Age",
                                            "last_bounty": "Latest Bounty",
                                            "Name": "Name"
                                            },
                                        height = height)
    fig_latest_age_to_bounty.update_xaxes(tickangle=0)
    fig_latest_age_to_bounty.update_layout(
                                            xaxis_title="Age",
                                            yaxis_title="Bounty Amount",
                                            )
    return fig_latest_age_to_bounty

@st.cache_data()
def fig_age_to_bounty_by_crew(df_age_bounty, height):
    fig_age_to_bounty_by_crew = px.scatter(x = df_age_bounty['latest_age'], 
                                        y=df_age_bounty['last_bounty'], 
                                        color = df_age_bounty['main_crew'],
                                        labels={
                                            "latest_age": "Age",
                                            "last_bounty": "Latest Bounty",
                                            "main_crew": "Crew"
                                            },
                                            height = height)
    fig_age_to_bounty_by_crew.update_xaxes(tickangle=0)
    fig_age_to_bounty_by_crew.update_layout(
                                            xaxis_title="Age",
                                            yaxis_title="Bounty Amount",
                                            )
    return fig_age_to_bounty_by_crew

def main():
    appearance_df, char_details_df, df_age_bounty = generate_df()
    # st.set_page_config(layout="wide")
    height = 650

    st.markdown(""" <style>
                #MainMenu {visibility: hidden;}
                footer {visibility: hidden;}
                </style> """, 
                unsafe_allow_html=True
                )

    # Select Plot Option
    st.sidebar.markdown("## Character Appearance in each chapter")
    char_appearance = st.sidebar.checkbox('Top 20 Character Appearance', value = True)
    char_appearance_sunburst = st.sidebar.checkbox('Top 10 Character Appearance (Sunburst)', value = True)

    st.sidebar.markdown("## Bounty")
    char_bounty = st.sidebar.checkbox('Bounties (Descending order)', value = True)
    latest_bounty = st.sidebar.checkbox('Bounty Distribution', value = False)
    latest_age_to_bounty = st.sidebar.checkbox('Latest Bounty by age', value = False)
    age_to_bounty_by_crew = st.sidebar.checkbox('Latest Bounty grouped by crew', value = False)

    if char_appearance:
        st.write("## Top 20 Character Appearance")
        st.plotly_chart(fig_app_by_arc(appearance_df, height),use_container_width=True)

    if char_appearance_sunburst:
        st.write("## Top 10 Character Appearance!")
        st.write("### Click on the name to expand on their info!")
        st.plotly_chart(fig_app_by_arc_sunburst(appearance_df),use_container_width=True)

    if char_bounty:
        st.write("## Top 50 Latest bounty (log scaled)")
        st.plotly_chart(fig_latest_bounty(char_details_df, height),use_container_width=True)

    if latest_bounty:
        st.write("## Top 50 Crews by Bounty")
        st.plotly_chart(fig_latest_bounty_dist(char_details_df, height),use_container_width=True)

    if latest_age_to_bounty:
        st.write("## Bounty by Age")
        st.plotly_chart(fig_latest_age_to_bounty(df_age_bounty,height),use_container_width=True)

    if age_to_bounty_by_crew:
        st.write("## Bounty by Age grouped by Crew")
        st.plotly_chart(fig_age_to_bounty_by_crew(df_age_bounty, height), use_container_width=True)

if __name__ == "__main__":
    main()