comparison utils.py @ 0:915447b14520 draft

planemo upload for repository https://github.com/goeckslab/Galaxy-Pycaret commit d79b0f722b7d09505a526d1a4332f87e548a3df1
author goeckslab
date Wed, 11 Dec 2024 05:00:00 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:915447b14520
1 import base64
2 import logging
3
4 logging.basicConfig(level=logging.DEBUG)
5 LOG = logging.getLogger(__name__)
6
7
8 def get_html_template():
9 return """
10 <html>
11 <head>
12 <title>Model Training Report</title>
13 <style>
14 body {
15 font-family: Arial, sans-serif;
16 margin: 0;
17 padding: 20px;
18 background-color: #f4f4f4;
19 }
20 .container {
21 max-width: 800px;
22 margin: auto;
23 background: white;
24 padding: 20px;
25 box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
26 }
27 h1 {
28 text-align: center;
29 color: #333;
30 }
31 h2 {
32 border-bottom: 2px solid #4CAF50;
33 color: #4CAF50;
34 padding-bottom: 5px;
35 }
36 table {
37 width: 100%;
38 border-collapse: collapse;
39 margin: 20px 0;
40 }
41 table, th, td {
42 border: 1px solid #ddd;
43 }
44 th, td {
45 padding: 8px;
46 text-align: left;
47 }
48 th {
49 background-color: #4CAF50;
50 color: white;
51 }
52 .plot {
53 text-align: center;
54 margin: 20px 0;
55 }
56 .plot img {
57 max-width: 100%;
58 height: auto;
59 }
60 .tabs {
61 display: flex;
62 margin-bottom: 20px;
63 cursor: pointer;
64 justify-content: space-around;
65 }
66 .tab {
67 padding: 10px;
68 background-color: #4CAF50;
69 color: white;
70 border-radius: 5px 5px 0 0;
71 flex-grow: 1;
72 text-align: center;
73 margin: 0 5px;
74 }
75 .tab.active-tab {
76 background-color: #333;
77 }
78 .tab-content {
79 display: none;
80 padding: 20px;
81 border: 1px solid #ddd;
82 border-top: none;
83 background-color: white;
84 }
85 .tab-content.active-content {
86 display: block;
87 }
88 </style>
89 </head>
90 <body>
91 <div class="container">
92 """
93
94
95 def get_html_closing():
96 return """
97 </div>
98 <script>
99 function openTab(evt, tabName) {{
100 var i, tabcontent, tablinks;
101 tabcontent = document.getElementsByClassName("tab-content");
102 for (i = 0; i < tabcontent.length; i++) {{
103 tabcontent[i].style.display = "none";
104 }}
105 tablinks = document.getElementsByClassName("tab");
106 for (i = 0; i < tablinks.length; i++) {{
107 tablinks[i].className =
108 tablinks[i].className.replace(" active-tab", "");
109 }}
110 document.getElementById(tabName).style.display = "block";
111 evt.currentTarget.className += " active-tab";
112 }}
113 document.addEventListener("DOMContentLoaded", function() {{
114 document.querySelector(".tab").click();
115 }});
116 </script>
117 </body>
118 </html>
119 """
120
121
122 def customize_figure_layout(fig, margin_dict=None):
123 """
124 Update the layout of a Plotly figure to reduce margins.
125
126 Parameters:
127 fig (plotly.graph_objects.Figure): The Plotly figure to customize.
128 margin_dict (dict, optional): A dictionary specifying margin sizes.
129 Example: {'l': 10, 'r': 10, 't': 10, 'b': 10}
130
131 Returns:
132 plotly.graph_objects.Figure: The updated Plotly figure.
133 """
134 if margin_dict is None:
135 # Set default smaller margins
136 margin_dict = {'l': 40, 'r': 40, 't': 40, 'b': 40}
137
138 fig.update_layout(margin=margin_dict)
139 return fig
140
141
142 def add_plot_to_html(fig, include_plotlyjs=True):
143 custom_margin = {'l': 40, 'r': 40, 't': 60, 'b': 60}
144 fig = customize_figure_layout(fig, margin_dict=custom_margin)
145 return fig.to_html(full_html=False,
146 default_height=350,
147 include_plotlyjs="cdn" if include_plotlyjs else False)
148
149
150 def add_hr_to_html():
151 return "<hr>"
152
153
154 def encode_image_to_base64(image_path):
155 """Convert an image file to a base64 encoded string."""
156 with open(image_path, "rb") as img_file:
157 return base64.b64encode(img_file.read()).decode("utf-8")