Skip to content

Commit 194b6bf

Browse files
committed
view: add remote argument for hpc usage
1 parent 64bf38d commit 194b6bf

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

spdkit/__init__.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,9 @@ def view_in_agui(mols: Union[Molecule, List[Molecule]], sleep=5):
352352
# 4872ebff ends here
353353

354354
# [[file:../spdkit-python.note::db2ed389][db2ed389]]
355-
def view(mols: Union[Molecule, List[Molecule]]):
355+
def view(mols: Union[Molecule, List[Molecule]], remote=False):
356356
"""view molecule/molecules using gchemol-view"""
357-
import subprocess, tempfile, os
358-
import time
357+
import os
359358

360359
if isinstance(mols, Molecule):
361360
mols = [mols]
@@ -364,6 +363,25 @@ def view(mols: Union[Molecule, List[Molecule]]):
364363
else:
365364
mols = list(mols)
366365

367-
v = gui.GchemolViewClient()
368-
v.load(mols)
366+
# here we determine the port based on user id for avoiding port
367+
# conflicts between different users
368+
if remote:
369+
uid = os.getuid()
370+
port = 49152 + (uid % 1000)
371+
else:
372+
port = 3039
373+
viewer = gui.GchemolViewClient(port)
374+
try:
375+
viewer.load(mols)
376+
except:
377+
print(
378+
f"The gchemol remove view service is not ready on port http://127.0.0.1:{port}."
379+
)
380+
if remote:
381+
print(
382+
f"If you are in HPC environment, a safe port is for remote view is {port}"
383+
)
384+
print(
385+
"Follow the link for detailed setup for reverse port forwarding: https://github.com/ybyygu/gchemol-view"
386+
)
369387
# db2ed389 ends here

src/gui.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use reqwest::blocking::Client;
1010

1111
/// A client for remote view using gchemol-view
1212
#[pyclass(name = "GchemolViewClient")]
13+
#[pyo3(text_signature = "(symbol, position=[0, 0, 0])")]
1314
pub struct PyGchemolViewClient {
1415
client: Client,
1516
server: String,
@@ -18,11 +19,14 @@ pub struct PyGchemolViewClient {
1819
#[pymethods]
1920
impl PyGchemolViewClient {
2021
#[new]
21-
fn new() -> Self {
22+
#[pyo3(signature = (port = 3039))]
23+
fn new(port: usize) -> Self {
24+
let server = format!("127.0.0.1:{port}");
25+
info!("connecting to {server:?}");
2226
let client = reqwest::blocking::Client::builder().build().expect("reqwest client");
2327
Self {
2428
client: client.into(),
25-
server: "127.0.0.1:3039".to_owned(),
29+
server,
2630
}
2731
}
2832

0 commit comments

Comments
 (0)