From 8fcc151f6fcf535bb9d1e5d4870c4868badf5cac Mon Sep 17 00:00:00 2001 From: mvdbeek Date: Thu, 23 May 2024 13:15:01 +0200 Subject: [PATCH 1/2] Limit templated visualization data, fix PCA plot for tabular datasets --- .../PCA_3Dplot/templates/PCA_3Dplot.mako | 20 +++++++++++++------ .../ts_visjs/templates/ts_visjs.mako | 15 ++++++++++++-- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/config/plugins/visualizations/PCA_3Dplot/templates/PCA_3Dplot.mako b/config/plugins/visualizations/PCA_3Dplot/templates/PCA_3Dplot.mako index b85d9645932a..f36e0ea10fce 100644 --- a/config/plugins/visualizations/PCA_3Dplot/templates/PCA_3Dplot.mako +++ b/config/plugins/visualizations/PCA_3Dplot/templates/PCA_3Dplot.mako @@ -1,7 +1,11 @@ <%! +import csv import sys from io import StringIO -import csv + +from galaxy.exceptions import RequestParameterInvalidException + +MAX_SIZE = 100000 # 100 KB, empirically the largest value I can render on my browser (m2 mac, chrome) %> <% @@ -29,11 +33,15 @@ def create_options(header): return colour_options, start_options def load_data(): - input = "\n".join(list(hda.datatype.dataprovider(hda, 'line', comment_char=none, provide_blank=True, strip_lines=False, strip_newlines=True))) - tabular_file = StringIO(input) - dialect = csv.Sniffer().sniff(tabular_file.read(1024), delimiters=";,\t") - tabular_file.seek(0) - table = csv.reader(tabular_file, dialect) + lines = [] + size = 0 + for line in hda.datatype.dataprovider(hda, 'line', comment_char=none, provide_blank=True, strip_lines=False, strip_newlines=True): + size += len(line) + if size > MAX_SIZE: + raise RequestParameterInvalidException("Dataset too large to render, dataset must be less than 100 KB in size.") + lines.append(line) + dialect = csv.Sniffer().sniff("\n".join(lines)) + table = csv.reader(lines, dialect) data = "[" for i, row in enumerate(table): if i == 0: diff --git a/config/plugins/visualizations/ts_visjs/templates/ts_visjs.mako b/config/plugins/visualizations/ts_visjs/templates/ts_visjs.mako index 7504667c5347..e96868683401 100644 --- a/config/plugins/visualizations/ts_visjs/templates/ts_visjs.mako +++ b/config/plugins/visualizations/ts_visjs/templates/ts_visjs.mako @@ -3,6 +3,10 @@ import collections import json import sys from numpy import inf + +from galaxy.exceptions import RequestParameterInvalidException + +MAX_SIZE = 100000 # 100 KB %> <% @@ -58,8 +62,15 @@ def write_reaction(edge_id, left_index, right_index, substrates, products, rate, def load_data(): output = {"nodes": "", "border_nodes": "", "edges": "", "edges": "", "self_loops": ""} - data = ''.join(list(hda.datatype.dataprovider(hda, 'line', strip_lines=True, strip_newlines=True))) - data = json.loads(data) + lines = [] + size = 0 + + for line in hda.datatype.dataprovider(hda, 'line', strip_lines=True, strip_newlines=True): + size += len(line) + if size > MAX_SIZE: + raise RequestParameterInvalidException("Dataset too large to render, dataset must be less than 100 KB in size.") + lines.append(line) + data = json.loads("".join(lines)) ordering = data['ordering'] nodes = {int(key): to_counter(data['nodes'][key], ordering) for key in data['nodes'].keys()} From 15fa3a43f787cfa1f752d9ee7da45a88c2ec5af0 Mon Sep 17 00:00:00 2001 From: mvdbeek Date: Thu, 23 May 2024 13:15:26 +0200 Subject: [PATCH 2/2] Fix display of error messages in visualizations --- lib/galaxy/webapps/galaxy/controllers/visualization.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/galaxy/webapps/galaxy/controllers/visualization.py b/lib/galaxy/webapps/galaxy/controllers/visualization.py index 157cfde250d2..b70fc5ad6f85 100644 --- a/lib/galaxy/webapps/galaxy/controllers/visualization.py +++ b/lib/galaxy/webapps/galaxy/controllers/visualization.py @@ -242,7 +242,7 @@ def render(self, trans, visualization_name, embedded=None, **kwargs): try: return plugin.render(trans=trans, embedded=embedded, **kwargs) except Exception as exception: - self._handle_plugin_error(trans, visualization_name, exception) + return self._handle_plugin_error(trans, visualization_name, exception) def _get_plugin_from_registry(self, trans, visualization_name): """