|
| 1 | +# ---------------------------------------------------------------------------- |
| 2 | +# Copyright (c) 2023-2026, QIIME 2 development team. |
| 3 | +# |
| 4 | +# Distributed under the terms of the Modified BSD License. |
| 5 | +# |
| 6 | +# The full license is in the file LICENSE, distributed with this software. |
| 7 | +# ---------------------------------------------------------------------------- |
| 8 | + |
| 9 | + |
| 10 | +import importlib.resources |
| 11 | +import json |
| 12 | +import os |
| 13 | +import shutil |
| 14 | + |
| 15 | +import jinja2 |
| 16 | + |
| 17 | + |
| 18 | +_VENDORED_FILES = ( |
| 19 | + 'vega.min.js', |
| 20 | + 'vega-embed.min.js', |
| 21 | + 'LICENSE-vega', |
| 22 | + 'LICENSE-vega-embed', |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +def _copy_vendored_assets(output_dir): |
| 27 | + vendor_dir = importlib.resources.files( |
| 28 | + 'q2_vizard') / 'assets' / 'vendor' |
| 29 | + |
| 30 | + for filename in _VENDORED_FILES: |
| 31 | + source = vendor_dir / filename |
| 32 | + destination = os.path.join(output_dir, filename) |
| 33 | + |
| 34 | + with source.open('rb') as source_fh, \ |
| 35 | + open(destination, 'wb') as dest_fh: |
| 36 | + shutil.copyfileobj(source_fh, dest_fh) |
| 37 | + |
| 38 | + |
| 39 | +def _json_replace(json_obj, /, **values): |
| 40 | + """ |
| 41 | + Search for elements of `{"{{REPLACE_PARAM}}": "some_key"}` and replace |
| 42 | + with the result of `values["some_key"]`. |
| 43 | + Expects positional args for json_obj so as to avoid any future |
| 44 | + namespace collisions that may appear in the values dict |
| 45 | + """ |
| 46 | + if type(json_obj) is dict and list(json_obj) == ["{{REPLACE_PARAM}}"]: |
| 47 | + param_name = json_obj["{{REPLACE_PARAM}}"] |
| 48 | + return values[param_name] |
| 49 | + |
| 50 | + if type(json_obj) is list: |
| 51 | + return [_json_replace(x, **values) for x in json_obj] |
| 52 | + |
| 53 | + elif type(json_obj) is dict: |
| 54 | + return {key: _json_replace(value, **values) |
| 55 | + for key, value in json_obj.items()} |
| 56 | + |
| 57 | + else: |
| 58 | + return json_obj |
| 59 | + |
| 60 | + |
| 61 | +def _render_visualization(output_dir, name, spec_filename='spec.json', /, |
| 62 | + **spec_params): |
| 63 | + """ |
| 64 | + Base template for rendering all visualizations. |
| 65 | + Expects positional args for output_dir, name & spec_filename |
| 66 | + so as to avoid any future namespace collisions |
| 67 | + that may appear in the spec_params dict. |
| 68 | +
|
| 69 | + Assumes the same general file structure for each viz as shown below: |
| 70 | +
|
| 71 | + per-viz assets |
| 72 | + -------------- |
| 73 | + `q2_vizard/assets/{viz_name}` |
| 74 | +
|
| 75 | + index.html |
| 76 | + ---------- |
| 77 | + `q2_vizard/assets/{viz_name}/index.html` |
| 78 | +
|
| 79 | + spec(s) |
| 80 | + ------- |
| 81 | + `q2_vizard/assets/{viz_name}/{spec_name}.json` |
| 82 | + """ |
| 83 | + assets = importlib.resources.files('q2_vizard') / 'assets' / name |
| 84 | + |
| 85 | + with (assets / spec_filename).open() as fh: |
| 86 | + full_spec = _json_replace(json.load(fh), **spec_params) |
| 87 | + |
| 88 | + J_ENV = jinja2.Environment( |
| 89 | + loader=jinja2.PackageLoader('q2_vizard', f'assets/{name}') |
| 90 | + ) |
| 91 | + index = J_ENV.get_template('index.html') |
| 92 | + |
| 93 | + with open(os.path.join(output_dir, 'index.html'), 'w') as fh: |
| 94 | + spec_string = json.dumps(full_spec) |
| 95 | + fh.write(index.render(spec=spec_string)) |
| 96 | + |
| 97 | + _copy_vendored_assets(output_dir) |
0 commit comments