Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tmpdir for povray rendering #675

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions aiidalab_widgets_base/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import base64
import copy
import os
import pathlib
import re
import warnings

Expand Down Expand Up @@ -868,6 +870,7 @@ def _download_tab(self):

def _render_structure(self, change=None):
"""Render the structure with POVRAY."""
from tempfile import TemporaryDirectory

if not isinstance(self.displayed_structure, ase.Atoms):
return
Expand Down Expand Up @@ -957,16 +960,24 @@ def _render_structure(self, change=None):

scene = vapory.Scene(camera, objects=objects)
fname = bb.get_chemical_formula() + ".png"
scene.render(
fname,
width=2560,
height=1440,
antialiasing=0.000,
quality=11,
remove_temp=False,
)
with open(fname, "rb") as raw:
payload = base64.b64encode(raw.read()).decode()

with TemporaryDirectory() as tmpdir:
cwd = pathlib.Path.cwd()
try:
os.chdir(tmpdir)
scene.render(
fname,
width=2560,
height=1440,
antialiasing=0.000,
quality=11,
remove_temp=False,
)
with open(fname, "rb") as raw:
payload = base64.b64encode(raw.read()).decode()
finally:
os.chdir(cwd)

self._download(payload=payload, filename=fname)
self.render_btn.disabled = False

Expand Down
6 changes: 6 additions & 0 deletions tests/test_viewers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

import ase
import pytest
import traitlets as tl
Expand Down Expand Up @@ -107,6 +109,10 @@ def test_structure_data_viewer_storage(structure_data_object):
# fmt: on
v._render_structure()

# Make sure we don't polute current working dir with tempfiles
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, but is this necessary for the test to pass? If it pollutes the working dir, will the test fail?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's a regression test for this PR.

assert not Path("__temp__.pov").exists()
assert not Path("Si2.png").exists()


@pytest.mark.usefixtures("aiida_profile_clean")
def test_structure_data_viewer_selection(structure_data_object):
Expand Down
Loading