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

add insert function #208

Merged
merged 7 commits into from
Sep 16, 2023
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
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions zndraw/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,8 @@ def draw_schema():
@io.on("atoms:delete")
def delete_atoms(data):
emit("atoms:delete", data, broadcast=True, include_self=False)


@io.on("atoms:insert")
def insert_atoms(data):
emit("atoms:insert", data, broadcast=True, include_self=False)
12 changes: 6 additions & 6 deletions zndraw/static/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions zndraw/static/pycom/Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,35 @@ class Cache {
).innerHTML = `${slider.value} / ${slider.max}`;
});

this._socket.on("atoms:insert", (data) => {
// move all keys after id one step forward
const remainingKeys = Object.keys(this._cache);
const id = parseInt(Object.keys(data)[0]);
for (let i = remainingKeys.length - 1; i >= id; i--) {
const currentKey = remainingKeys[i];
const newIndex = i + 1;
this._cache[newIndex] = this._cache[currentKey];
delete this._cache[currentKey];
}
// insert new atoms
this._cache[id] = new Atoms({
positions: data[id].positions,
cell: data[id].cell,
numbers: data[id].numbers,
colors: data[id].colors,
radii: data[id].radii,
connectivity: data[id].connectivity,
calc: data[id].calc,
pbc: data[id].pbc,
});
// update slider
const slider = document.getElementById("frame-slider");
slider.max = Object.keys(this._cache).length - 1;
document.getElementById(
"info",
).innerHTML = `${slider.value} / ${slider.max}`;
});

this._socket.on("atoms:download", (ids) => {
// send all atoms at once
const data = {};
Expand Down
4 changes: 3 additions & 1 deletion zndraw/zndraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ def display(self, index):

def insert(self, index, value):
"""Insert atoms before index"""
raise NotImplementedError
self.socket.emit("atoms:insert", {index: atoms_to_json(value)})
if self.display_new:
self.display(index)

def append(self, value: ase.Atoms) -> None:
"""Append atoms to the end of the list"""
Expand Down
Loading