Mercurial > repos > goeckslab > ludwig_experiment
comparison utils.py @ 0:78e6686a218e draft
planemo upload for repository https://github.com/goeckslab/Galaxy-Ludwig.git commit bdea9430787658783a51cc6c2ae951a01e455bb4
| author | goeckslab |
|---|---|
| date | Tue, 07 Jan 2025 22:45:39 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:78e6686a218e |
|---|---|
| 1 import base64 | |
| 2 import json | |
| 3 | |
| 4 | |
| 5 def get_html_template(): | |
| 6 return """ | |
| 7 <html> | |
| 8 <head> | |
| 9 <title>Galaxy-Ludwig Report</title> | |
| 10 <style> | |
| 11 body { | |
| 12 font-family: Arial, sans-serif; | |
| 13 margin: 0; | |
| 14 padding: 20px; | |
| 15 background-color: #f4f4f4; | |
| 16 } | |
| 17 .container { | |
| 18 max-width: 800px; | |
| 19 margin: auto; | |
| 20 background: white; | |
| 21 padding: 20px; | |
| 22 box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); | |
| 23 overflow-x: auto; | |
| 24 } | |
| 25 h1 { | |
| 26 text-align: center; | |
| 27 color: #333; | |
| 28 } | |
| 29 h2 { | |
| 30 border-bottom: 2px solid #4CAF50; | |
| 31 color: #4CAF50; | |
| 32 padding-bottom: 5px; | |
| 33 } | |
| 34 table { | |
| 35 border-collapse: collapse; | |
| 36 margin: 20px 0; | |
| 37 width: 100%; | |
| 38 table-layout: fixed; /* Enforces consistent column widths */ | |
| 39 } | |
| 40 table, th, td { | |
| 41 border: 1px solid #ddd; | |
| 42 } | |
| 43 th, td { | |
| 44 padding: 8px; | |
| 45 text-align: center; /* Center-align text */ | |
| 46 vertical-align: middle; /* Center-align content vertically */ | |
| 47 word-wrap: break-word; /* Break long words to avoid overflow */ | |
| 48 } | |
| 49 th:first-child, td:first-child { | |
| 50 width: 5%; /* Smaller width for the first column */ | |
| 51 } | |
| 52 th:nth-child(2), td:nth-child(2) { | |
| 53 width: 50%; /* Wider for the metric/description column */ | |
| 54 } | |
| 55 th:last-child, td:last-child { | |
| 56 width: 25%; /* Value column gets remaining space */ | |
| 57 } | |
| 58 th { | |
| 59 background-color: #4CAF50; | |
| 60 color: white; | |
| 61 } | |
| 62 .plot { | |
| 63 text-align: center; | |
| 64 margin: 20px 0; | |
| 65 } | |
| 66 .plot img { | |
| 67 max-width: 100%; | |
| 68 height: auto; | |
| 69 } | |
| 70 </style> | |
| 71 </head> | |
| 72 <body> | |
| 73 <div class="container"> | |
| 74 """ | |
| 75 | |
| 76 | |
| 77 def get_html_closing(): | |
| 78 return """ | |
| 79 </div> | |
| 80 </body> | |
| 81 </html> | |
| 82 """ | |
| 83 | |
| 84 | |
| 85 def encode_image_to_base64(image_path): | |
| 86 """Convert an image file to a base64 encoded string.""" | |
| 87 with open(image_path, "rb") as img_file: | |
| 88 return base64.b64encode(img_file.read()).decode("utf-8") | |
| 89 | |
| 90 | |
| 91 def json_to_nested_html_table(json_data, depth=0): | |
| 92 """ | |
| 93 Convert JSON object to an HTML nested table. | |
| 94 | |
| 95 Parameters: | |
| 96 json_data (dict or list): The JSON data to convert. | |
| 97 depth (int): Current depth level for indentation. | |
| 98 | |
| 99 Returns: | |
| 100 str: HTML string for the nested table. | |
| 101 """ | |
| 102 # Base case: if JSON is a simple key-value pair dictionary | |
| 103 if isinstance(json_data, dict) and all( | |
| 104 not isinstance(v, (dict, list)) for v in json_data.values() | |
| 105 ): | |
| 106 # Render a flat table | |
| 107 rows = [ | |
| 108 f"<tr><th>{key}</th><td>{value}</td></tr>" | |
| 109 for key, value in json_data.items() | |
| 110 ] | |
| 111 return f"<table>{''.join(rows)}</table>" | |
| 112 | |
| 113 # Base case: if JSON is a list of simple values | |
| 114 if isinstance(json_data, list) and all( | |
| 115 not isinstance(v, (dict, list)) for v in json_data | |
| 116 ): | |
| 117 rows = [ | |
| 118 f"<tr><th>Index {i}</th><td>{value}</td></tr>" | |
| 119 for i, value in enumerate(json_data) | |
| 120 ] | |
| 121 return f"<table>{''.join(rows)}</table>" | |
| 122 | |
| 123 # Recursive case: if JSON contains nested structures | |
| 124 if isinstance(json_data, dict): | |
| 125 rows = [ | |
| 126 f"<tr><th style='padding-left:{depth * 20}px;'>{key}</th>" | |
| 127 f"<td>{json_to_nested_html_table(value, depth + 1)}</td></tr>" | |
| 128 for key, value in json_data.items() | |
| 129 ] | |
| 130 return f"<table>{''.join(rows)}</table>" | |
| 131 | |
| 132 if isinstance(json_data, list): | |
| 133 rows = [ | |
| 134 f"<tr><th style='padding-left:{depth * 20}px;'>[{i}]</th>" | |
| 135 f"<td>{json_to_nested_html_table(value, depth + 1)}</td></tr>" | |
| 136 for i, value in enumerate(json_data) | |
| 137 ] | |
| 138 return f"<table>{''.join(rows)}</table>" | |
| 139 | |
| 140 # Base case: simple value | |
| 141 return f"{json_data}" | |
| 142 | |
| 143 | |
| 144 def json_to_html_table(json_data): | |
| 145 """ | |
| 146 Convert JSON to a vertically oriented HTML table. | |
| 147 | |
| 148 Parameters: | |
| 149 json_data (str or dict): JSON string or dictionary. | |
| 150 | |
| 151 Returns: | |
| 152 str: HTML table representation. | |
| 153 """ | |
| 154 if isinstance(json_data, str): | |
| 155 json_data = json.loads(json_data) | |
| 156 return json_to_nested_html_table(json_data) |
