Py.Cafe

JiaYuanChng/

CO2capture

Vizro Interactive Iris Exploration

DocsPricing
  • data/
  • app.py
  • requirements.txt
app.py
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import vizro.models as vm
import plotly.graph_objects as go
import vizro.plotly.express as px
from vizro import Vizro
from vizro.tables import dash_ag_grid
from vizro.models.types import capture
from vizro.figures import kpi_card
import dash
from dash import dcc, html, Input, Output, callback, clientside_callback
import pandas as pd
import copy

df_article = pd.read_csv("./data/article_properties_17June.csv")
df_mat = pd.read_csv('data/elsevier_rsc_wiley_properties.csv')

@capture("graph")
def custom_hist(data_frame, x):
    fig = px.histogram(data_frame, x=x)
    fig.update_layout(#width=500, height=400, 
                      xaxis_title=None,
                      xaxis=dict(tickfont=dict(size=16, color='black')),
                      yaxis=dict(tickfont=dict(size=16, color='black'))
                     )
    return fig


@capture("graph")
def custom_scatter_dual(data_frame):
    data_frame = data_frame.dropna(subset=['maximum_capacity']).sort_values(by='maximum_capacity', ascending=True)
    
    fig = px.scatter(
        data_frame, 
        x='maximum_capacity', 
        y='BET',
        color='class',
        trendline="ols",
        trendline_scope='overall',
        marginal_x="histogram", 
        marginal_y="histogram",
    )
    
    # fig.data[0].update(
    #     # marker=dict(size=10, color='blue'),
    #     customdata=data_frame[['material', 'class', 'stability', 'cycle', 'doi','url']].values,
    #     hovertemplate='<b>Material:</b> %{customdata[0]}<br>' +
    #             '<b>BET:</b> %{y}<br>' +
    #             '<b>Capacity:</b> %{x}<br>' +
    #             '<b>Class:</b> %{customdata[1]}<br>' +
    #             '<b>Stability:</b> %{customdata[2]}<br>' +
    #             '<b>Cycle:</b> %{customdata[3]}<br>' +
    #             '<b>DOI:</b> %{customdata[4]}<extra></extra>'
    # )

    for i, trace in enumerate(fig.data):
        # Only update scatter plot traces (not histogram traces)
        # if i < len(data_frame['class'].unique()):
        if hasattr(trace, 'mode') and 'markers' in trace.mode:
            # Check if this is a main plot trace (not marginal)
            if (not hasattr(trace, 'xaxis') or trace.xaxis == 'x') and \
            (not hasattr(trace, 'yaxis') or trace.yaxis == 'y'):
                trace.update(
                    marker=dict(size=10),
                    customdata=data_frame[['material', 'class', 'stability', 'cycle', 'doi','url']].values,
                    hovertemplate='<b>Material:</b> %{customdata[0]}<br>' +
                            '<b>BET:</b> %{y}<br>' +
                            '<b>Capacity:</b> %{x}<br>' +
                            '<b>Class:</b> %{customdata[1]}<br>' +
                            '<b>Stability:</b> %{customdata[2]}<br>' +
                            '<b>Cycle:</b> %{customdata[3]}<br>' +
                            '<b>DOI:</b> %{customdata[4]}<extra></extra>'
                )
    
    # Update layout
    fig.update_layout(
        xaxis_title="Maximum capacity (mmol/g)",
        yaxis_title="BET (m2/g)",
        xaxis=dict(tickfont=dict(size=16, color='black')),
        yaxis=dict(tickfont=dict(size=16, color='black'))
    )

    fig2 = copy.deepcopy(fig)
    fig2.data[-3].showlegend = True
    fig2.data = fig2.data[:-2]
    
    return fig2

@capture("graph")
def custom_scatter_click(data_frame, target, info_to_show, width=500, height=400):
    data_frame = data_frame.dropna(subset=[target]).sort_values(by=target, ascending=True)
    fig = go.Figure()
    if target=='BET':
        fig.add_trace(go.Scatter(
            x=list(range(len(data_frame))),  
            y=data_frame[target],
            mode='markers',
            marker=dict(size=10, color='blue'),
            customdata=data_frame[info_to_show].values,
            hovertemplate='<b>Material:</b> %{customdata[0]}<br>' +
                    '<b>BET:</b> %{y}<br>' +
                    '<b>Capacity:</b> %{customdata[1]}<br>' +
                    '<b>Class:</b> %{customdata[2]}<br>' +
                    '<b>Stability:</b> %{customdata[3]}<br>' +
                    '<b>Cycle:</b> %{customdata[4]}<br>' +
                    '<b>DOI:</b> %{customdata[5]}<extra></extra>'
            )
            )
        fig.update_layout(xaxis_title="Index",
                        yaxis_title="BET (m2/g)",
                        xaxis=dict(tickfont=dict(size=16, color='black')),
                        yaxis=dict(tickfont=dict(size=16, color='black'))
                        )

    if target=='maximum_capacity':
        fig.add_trace(go.Scatter(
            x=list(range(len(data_frame))),  
            y=data_frame[target],
            mode='markers',
            marker=dict(size=10, color='blue'),
            customdata=data_frame[info_to_show].values,
            hovertemplate='<b>Material:</b> %{customdata[0]}<br>' +
                    '<b>BET:</b> %{customdata[1]}<br>' +
                    '<b>Capacity:</b> %{y}<br>' +
                    '<b>Class:</b> %{customdata[2]}<br>' +
                    '<b>Stability:</b> %{customdata[3]}<br>' +
                    '<b>Cycle:</b> %{customdata[4]}<br>' +
                    '<b>DOI:</b> %{customdata[5]}<extra></extra>'
            )
            )
        fig.update_layout(width=width, height=height, xaxis_title="Index",
                        yaxis_title="Capacity (mmol/g)",
                        xaxis=dict(tickfont=dict(size=16, color='black')),
                        yaxis=dict(tickfont=dict(size=16, color='black'))
                        )

    return fig



first_page = vm.Page(
    title="Article Data",
    components=[
        vm.AgGrid(
            figure=dash_ag_grid(df_article),
        ),
    ],
)

second_page = vm.Page(
    title="Material Data",
    components=[
        vm.AgGrid(
            figure=dash_ag_grid(df_mat),
        ),
    ],
)

third_page = vm.Page(
    title="Classification",
    layout=vm.Grid(grid=[[0, 1,-1,-1], [2, 3,-1,-1], [4, 5,-1,-1], [6, 7,-1,-1]], 
                   row_gap="40px", col_gap="40px", 
                   row_min_height='450px', col_min_width='450px'),
    components = [
        vm.Graph(figure=custom_hist(df_article, x='Year'),title="Year"),
        vm.Graph(
            figure=custom_hist(df_article, x='exp_classify'), 
            title="Article contains experimental work"
            ),
        vm.Graph(
            figure=custom_hist(df_article, x='mat_classify'), 
            title="Material relevant to CO2 capture"
            ),
        vm.Graph(
            figure=custom_hist(df_article, x='source_classify'), 
            title="CO2 source"
            ),
        vm.Graph(
            figure=custom_hist(df_article, x='ads_classify'), 
            title="CO2 capture mechanism"
            ),
        vm.Graph(figure=custom_hist(df_mat, 'class'), title="Adsorbent classification"),
        vm.Graph(figure=custom_hist(df_mat, 'cycle'), title="Cyclic experiments or regenerations"),
        vm.Graph(figure=custom_hist(df_mat, 'stability'), title="Stability"),
    ]
)

fourth_page = vm.Page(
    title="Analysis",
    layout=vm.Grid(grid=[[0, -1], [1, -1], [2, -1]],
                   row_gap="10px", col_gap="20px", 
                   row_min_height='550px', col_min_width='550px'),
    
    components = [
        
        vm.Graph(figure=custom_scatter_click(df_mat, 'maximum_capacity', 
                                             ['material','BET', 'class', 'stability', 'cycle', 'doi','url'], 
                                             width=550,height=500
                                            ), 
                 title="Capacity"
                ),
        vm.Graph(figure=custom_scatter_click(df_mat, 'BET', 
                                             ['material','maximum_capacity', 'class', 'stability', 'cycle', 'doi','url']
                                            ),
                 title="BET"
                ),
        vm.Graph(figure=custom_scatter_dual(df_mat),
                 title="Capacity vs. BET"
                )
    ],

    controls = [vm.Filter(column="class", selector=vm.Checklist())]


)


dashboard = vm.Dashboard(
    pages=[first_page, second_page, third_page, fourth_page],
    title = "Materials for CO2 Capture from Literatures Dashboard",
    navigation=vm.Navigation(nav_selector=vm.NavBar(
        items = [
            vm.NavLink(label='Data', pages=["Article Data", "Material Data"], icon="database"),
            vm.NavLink(label='Charts', pages=["Classification", "Analysis"], icon="bar_chart"),
            ]
            )
        )
    )



Vizro().build(dashboard).run()