Skip to content

Commit d190682

Browse files
committed
Use haddock3-ui bundle + offline second half
1 parent 28e3a27 commit d190682

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

src/haddock/clis/cli_analyse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ def analyse_step(
490490
offline=offline,
491491
)
492492
tables = clt_table_handler(clt_file, ss_file, is_cleaned)
493-
report_generator(boxes, scatters, tables, step)
493+
report_generator(boxes, scatters, tables, step, offline)
494494
# provide a zipped archive of the top ranked structures
495495
zip_top_ranked(ss_file, cluster_ranking, Path("summary.tgz"))
496496

src/haddock/libs/libplots.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -936,10 +936,15 @@ def clt_table_handler(clt_file, ss_file, is_cleaned=False):
936936
return df_merged
937937

938938

939-
def _css_styles_for_report():
939+
def _css_styles_for_report(offline: bool) -> str:
940940
"""
941941
Generate custom CSS styles for an analysis report.
942942
943+
Parameters
944+
----------
945+
offline : bool
946+
If True, the HTML will be generated for offline use.
947+
943948
Returns
944949
-------
945950
The CSS styles as a string.
@@ -977,12 +982,15 @@ def _css_styles_for_report():
977982
background-color: #f2f2f2
978983
}
979984
"""
980-
css_link = "https://esm.sh/@i-vresse/haddock3-ui@~0.2.2/dist/index.css"
985+
css_link = "https://cdn.jsdelivr.net/npm/@i-vresse/haddock3-ui@~0.3.0/dist/index.css"
986+
if offline:
987+
# TODO copy the css file to the report directory
988+
css_link = "../../data/ui/index.css"
981989
table_css = f' <link href="{css_link}" rel="stylesheet" />'
982990
return f"{table_css}<style>{custom_css}</style>"
983991

984992

985-
def _generate_html_report(step, figures):
993+
def _generate_html_report(step, figures, offline):
986994
"""
987995
Generate an HTML report for a specific step of analysis, including figures.
988996
@@ -994,27 +1002,31 @@ def _generate_html_report(step, figures):
9941002
A list of figures to include in the HTML body.
9951003
Each figure can be either a string representing a table or a
9961004
plotly.graph_objects.Figure object.
1005+
offline : bool
1006+
If True, the HTML will be generated for offline use.
9971007
9981008
Returns
9991009
-------
10001010
html_report : str
10011011
The generated HTML report as a string.
10021012
"""
10031013
html_report = "<!DOCTYPE html><html lang='en'>"
1004-
html_report += _generate_html_head(step)
1005-
html_report += _generate_html_body(figures)
1014+
html_report += _generate_html_head(step, offline)
1015+
html_report += _generate_html_body(figures, offline)
10061016
html_report += "</html>"
10071017
return html_report
10081018

10091019

1010-
def _generate_html_head(step):
1020+
def _generate_html_head(step, offline):
10111021
"""
10121022
Generate the HTML head section for an analysis report.
10131023
10141024
Parameters
10151025
----------
10161026
step : str
10171027
The step number.
1028+
offline : bool
1029+
If True, the HTML will be generated for offline use.
10181030
10191031
Returns
10201032
-------
@@ -1024,14 +1036,15 @@ def _generate_html_head(step):
10241036
head = "<head>"
10251037
head += f"<title>Analysis report of step {step}</title>"
10261038
head += f"<p class='title'>Analysis report of step {step}</p>"
1027-
head += _css_styles_for_report()
1039+
head += _css_styles_for_report(offline)
10281040
head += "</head>"
10291041
return head
10301042

10311043

10321044
def _generate_unclustered_table_html(
10331045
table_id: str,
10341046
df: pd.DataFrame,
1047+
bundle_url: str,
10351048
) -> str:
10361049
data = df.to_json(orient='records')
10371050
headers = [
@@ -1055,21 +1068,18 @@ def _generate_unclustered_table_html(
10551068
}}
10561069
</script>
10571070
<script type="module">
1058-
import {{ createRoot }} from "https://esm.sh/react-dom";
1059-
import {{ createElement }} from "https://esm.sh/react";
1060-
import {{ StructureTable }} from "https://esm.sh/@i-vresse/haddock3-ui@~0.2.2/dist/table/StructureTable?bundle-deps";
1071+
import {{ renderClusterTable }} from "{bundle_url}";
10611072
10621073
const props = JSON.parse(document.getElementById("data{table_id}").text)
10631074
1064-
createRoot(document.getElementById('{table_id}')).render(
1065-
createElement(StructureTable, props)
1066-
)
1075+
renderClusterTable(document.getElementById('{table_id}'), props.headers, props.structures)
10671076
</script>""" # noqa : E501
10681077

10691078

10701079
def _generate_clustered_table_html(
10711080
table_id: str,
10721081
df: pd.DataFrame,
1082+
bundle_url: str,
10731083
) -> str:
10741084
data = df.to_json(orient='records')
10751085
nr_best_columns = df.filter(like="best").shape[1]
@@ -1105,15 +1115,10 @@ def _generate_clustered_table_html(
11051115
}}
11061116
</script>
11071117
<script type="module">
1108-
import {{ createRoot }} from "https://esm.sh/react-dom";
1109-
import {{ createElement }} from "https://esm.sh/react";
1110-
import {{ ClusterTable }} from "https://esm.sh/@i-vresse/haddock3-ui@~0.2.2/dist/table/ClusterTable?bundle-deps";
1111-
1118+
import {{ renderClusterTable }} from "{bundle_url}";
11121119
const props = JSON.parse(document.getElementById("data{table_id}").text)
11131120
1114-
createRoot(document.getElementById('{table_id}')).render(
1115-
createElement(ClusterTable, props)
1116-
)
1121+
renderClusterTable(document.getElementById('{table_id}'), props.headers, props.clusters);
11171122
</script>""" # noqa : E501
11181123

11191124

@@ -1127,6 +1132,8 @@ def _generate_html_body(figures: list[Figure], offline: bool = False) -> str:
11271132
A list of figures to include in the HTML body.
11281133
Each figure can be either a string representing a table or a
11291134
plotly.graph_objects.Figure object.
1135+
offline : bool
1136+
If True, the HTML will be generated for offline use.
11301137
11311138
Returns
11321139
-------
@@ -1142,10 +1149,14 @@ def _generate_html_body(figures: list[Figure], offline: bool = False) -> str:
11421149
table_id = f"table{table_index}"
11431150

11441151
is_unclustered = 'cluster_rank' not in figure
1152+
bundle_url = "https://cdn.jsdelivr.net/npm/@i-vresse/haddock3-ui@~0.3.0/dist/report.bundle.js"
1153+
if offline:
1154+
# TODO copy the bundle to the run_dir folder
1155+
bundle_url = "../../data/ui/report.bundle.js"
11451156
if is_unclustered:
1146-
inner_html = _generate_unclustered_table_html(table_id, figure)
1157+
inner_html = _generate_unclustered_table_html(table_id, figure, bundle_url)
11471158
else:
1148-
inner_html = _generate_clustered_table_html(table_id, figure)
1159+
inner_html = _generate_clustered_table_html(table_id, figure, bundle_url)
11491160
else: # plots
11501161
inner_json = figure.to_json()
11511162
inner_html = create_html(
@@ -1162,7 +1173,7 @@ def _generate_html_body(figures: list[Figure], offline: bool = False) -> str:
11621173
return body
11631174

11641175

1165-
def report_generator(boxes, scatters, tables, step):
1176+
def report_generator(boxes, scatters, tables, step, offline):
11661177
"""
11671178
Create a figure include plots and tables.
11681179
@@ -1177,6 +1188,8 @@ def report_generator(boxes, scatters, tables, step):
11771188
list of scatter plots generated by scatter_plot_handler
11781189
table: list
11791190
a list including tables generated by clt_table_handler
1191+
offline: bool
1192+
If True, the HTML will be generated for offline use.
11801193
"""
11811194
figures = [tables]
11821195
# Combine scatters
@@ -1191,7 +1204,7 @@ def report_generator(boxes, scatters, tables, step):
11911204
figures.append(report_plots_handler(boxes))
11921205

11931206
# Write everything to a html file
1194-
html_report = _generate_html_report(step, figures)
1207+
html_report = _generate_html_report(step, figures, offline)
11951208
with open("report.html", "w", encoding="utf-8") as report:
11961209
report.write(html_report)
11971210

0 commit comments

Comments
 (0)