Skip to content

Commit

Permalink
fix(html): Add tool for HTML export
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Jun 26, 2023
1 parent a79d7fe commit 3705560
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .fetch_externals.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mkdir -p ./trame_vtk/modules/common/serve
curl https://unpkg.com/vue-vtk-js@3.1.6 -Lo ./trame_vtk/modules/common/serve/trame-vtk.js
curl https://kitware.github.io/vtk-js/examples/OfflineLocalView/OfflineLocalView.html -Lo ./trame_vtk/tools/static_viewer.html
6 changes: 2 additions & 4 deletions .github/workflows/test_and_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ jobs:

- name: Install trame-vtk.js
run: |
mkdir -p ./trame_vtk/modules/common/serve
curl https://unpkg.com/vue-vtk-js@3.1.6 -Lo ./trame_vtk/modules/common/serve/trame-vtk.js
bash .fetch_externals.sh
- name: Install dependencies
run: |
Expand Down Expand Up @@ -103,8 +102,7 @@ jobs:

- name: Install trame-vtk.js
run: |
mkdir -p ./trame_vtk/modules/common/serve
curl https://unpkg.com/vue-vtk-js@3.1.6 -Lo ./trame_vtk/modules/common/serve/trame-vtk.js
bash .fetch_externals.sh
- name: Python Semantic Release
uses: relekang/python-semantic-release@master
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
graft trame_vtk/modules/common/serve
include trame_vtk/LICENSE
include trame_vtk/tools/static_viewer.html
3 changes: 1 addition & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ To update the client code, run the following command line while updating the tar

.. code-block:: console
mkdir -p ./trame_vtk/modules/common/serve
curl https://unpkg.com/vue-vtk-js@3.1.5 -Lo ./trame_vtk/modules/common/serve/trame-vtk.js
bash .fetch_externals.sh
Trame widgets
Expand Down
1 change: 1 addition & 0 deletions trame/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
4 changes: 4 additions & 0 deletions trame/tools/vtksz2html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from trame_vtk.tools.vtksz2html import main

if __name__ == "__main__":
main()
Empty file added trame_vtk/tools/__init__.py
Empty file.
60 changes: 60 additions & 0 deletions trame_vtk/tools/vtksz2html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import argparse
import base64
from pathlib import Path
import sys

HTML_VIEWER_PATH = Path(__file__).with_name("static_viewer.html")


def embbed_data_to_viewer(input_data_file_path):
input = Path(input_data_file_path)
output = input.with_name(f"{input.name}.html")
base64Content = ""

if input.exists():
with open(input, "rb") as data:
dataContent = data.read()
base64Content = base64.b64encode(dataContent)
base64Content = base64Content.decode().replace("\n", "")

with open(HTML_VIEWER_PATH, mode="r", encoding="utf-8") as srcHtml:
with open(output, mode="w", encoding="utf-8") as dstHtml:
for line in srcHtml:
if "</body>" in line:
dstHtml.write("<script>\n")
dstHtml.write(
"var container = document.querySelector('.content');\n"
)
dstHtml.write('var base64Str = "%s";\n\n' % base64Content)
dstHtml.write(
"OfflineLocalView.load(container, { base64Str });\n"
)
dstHtml.write("</script>\n")

dstHtml.write(line)


def main():
parser = argparse.ArgumentParser(description="HTML exporter for local view data")

parser.add_argument(
"--input",
help="Input data file",
)

args, _ = parser.parse_known_args()

if args.input is None:
parser.print_help()
sys.exit(0)

input_file = Path(args.input)
if not input_file.exists():
parser.print_help()
sys.exit(0)

embbed_data_to_viewer(input_file)


if __name__ == "__main__":
main()
8 changes: 8 additions & 0 deletions trame_vtk/widgets/vtk/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,9 @@ def capture_image(self, format="image/png", opts={}):
opts,
)

def release_resources(self):
self.__view = None


class VtkShareDataset(HtmlElement):
def __init__(self, children=None, **kwargs):
Expand Down Expand Up @@ -949,6 +952,11 @@ def capture_image(self, format="image/png", opts={}):
opts,
)

def release_resources(self):
self._server.controller.on_server_ready.discard(self.update)
self.__view = None
self._widgets = None


class VtkView(HtmlElement):
def __init__(self, children=None, ref="view", **kwargs):
Expand Down

0 comments on commit 3705560

Please sign in to comment.