Skip to content

Commit

Permalink
add ZnTrack option (#249)
Browse files Browse the repository at this point in the history
* add ZnTrack option

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
PythonFZ and pre-commit-ci[bot] authored Oct 30, 2023
1 parent 97384e2 commit 9ca5b9a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
13 changes: 12 additions & 1 deletion zndraw/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
@cli.command()
def main(
filename: Optional[str] = typer.Argument(
None, help="Path to the file which should be visualized in ZnDraw."
None,
help="Path to the file which should be visualized in ZnDraw. Can also be the name and attribute of a ZnTrack Node like 'MyNode.atoms' if at least '--remote .' is provided. ",
),
webview: bool = typer.Option(
True,
Expand Down Expand Up @@ -55,6 +56,14 @@ def main(
False,
help="Use a token to authenticate the ZnDraw server. This is useful if you are running ZnDraw as a server application.",
),
remote: str = typer.Option(
None,
help="URL to a ZnTrack repository to stream data from.",
),
rev: str = typer.Option(
None,
help="Revision of the ZnTrack repository to stream data from.",
),
):
"""Start the ZnDraw server.
Expand All @@ -75,4 +84,6 @@ def main(
compute_bonds=compute_bonds,
upgrade_insecure_requests=upgrade_insecure_requests,
use_token=use_token,
remote=remote,
rev=rev,
)
4 changes: 3 additions & 1 deletion zndraw/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ def view(
compute_bonds: bool,
use_token: bool = False,
upgrade_insecure_requests: bool = False,
remote: str = None,
rev: str = None,
):
if not use_token:
app.config["token"] = "notoken"
app.config["upgrade_insecure_requests"] = upgrade_insecure_requests
app.config["compute_bonds"] = compute_bonds
url = f"http://127.0.0.1:{port}"

file_io = FileIO(filename, start, stop, step)
file_io = FileIO(filename, start, stop, step, remote, rev)

proc = mp.Process(
target=ZnDrawDefault,
Expand Down
17 changes: 16 additions & 1 deletion zndraw/zndraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class FileIO:
start: int = 0
stop: int = None
step: int = 1
remote: str = None
rev: str = None


@dataclasses.dataclass
Expand Down Expand Up @@ -222,7 +224,20 @@ def read_data(self):
if self.file_io.name is None:
return

if pathlib.Path(self.file_io.name).suffix == ".h5":
if self.file_io.remote is not None:
node_name, attribute = self.file_io.name.split(".", 1)
try:
import zntrack

node = zntrack.from_rev(
node_name, remote=self.file_io.remote, rev=self.file_io.rev
)
generator = getattr(node, attribute)
except ImportError as err:
raise ImportError(
"You need to install ZnTrack to use the remote feature"
) from err
elif pathlib.Path(self.file_io.name).suffix == ".h5":
reader = znh5md.ASEH5MD(self.file_io.name)
generator = reader.get_atoms_list()
else:
Expand Down

0 comments on commit 9ca5b9a

Please sign in to comment.