Py.Cafe

jhsmit/

solara-molecular-clock-viewer

Interactive Molecular Clock Viewer

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# %%
from ipymolstar.molviewspec import DEFAULT_MVS_LOAD_OPTIONS, MolViewSpec
from molviewspec import create_builder, States, GlobalMetadata
import numpy as np


def axis_arrows(builder, opacity=1.0):
    return (
        builder.primitives(opacity=opacity)
        .arrow(
            start=(0, 0, 0),
            end=(1, 0, 0),
            color="red",
            show_end_cap=True,
        )
        .arrow(
            start=(0, 0, 0),
            end=(0, 1, 0),
            color="green",
            tooltip="Y",
            show_end_cap=True,
        )
        .arrow(
            start=(0, 0, 0),
            end=(0, 0, 1),
            color="blue",
            tooltip="Z",
            show_end_cap=True,
        )
    )


# %%

r = 2
N = 600
phi = np.linspace(0, 2 * np.pi, N, endpoint=False)
phi += np.pi / 2
phi %= 2 * np.pi
phi = phi[::-1]
x, y = r * np.cos(phi), r * np.sin(phi)

metadata = GlobalMetadata(title="clock", description="one-handed")
snapshots = []
for i, (xi, yi) in enumerate(zip(x, y)):
    builder = create_builder()
    axis_arrows(builder)
    builder.primitives().arrow(
        start=(0, 0, 0), end=(xi, yi, 0), color="black", show_end_cap=True
    )
    builder.primitives().sphere(center=(0, 0, 0), radius=0.1, color="black")
    # builder.camera(target=(0, 0, 0), position=(0, 0, 10), up=(0, 1, 0))
    snapshot = builder.get_snapshot(
        title=str(i), linger_duration_ms=50, transition_duration_ms=10
    )

    snapshots.append(snapshot)
    # msvj_data = builder.get_state()

states = States(
    snapshots=snapshots,
    metadata=metadata,
).json(indent=None, exclude_none=True)

# import urllib.parse
# encoded_states = urllib.parse.quote(states)
# url = f"https://molstar.org/viewer/?mvs-data={encoded_states}"
# print(url)

# %%


mvs_load_options = {"keepSnapshotCamera": True} | DEFAULT_MVS_LOAD_OPTIONS
page = MolViewSpec(msvj_data=states, mvs_load_options=mvs_load_options)
page

# %%