Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Oct 17, 2023
1 parent bdd7790 commit b4fbf31
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 61 deletions.
10 changes: 7 additions & 3 deletions misc/Sockets.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
## Main

### connect
- WebClient has a `session[token]` from routing through `\`, therefore it will be send to join the room `app.config["ROOM_HOSTS"][session[token]]`

- WebClient has a `session[token]` from routing through `\`, therefore it will
be send to join the room `app.config["ROOM_HOSTS"][session[token]]`

### disconnect

### join(token)
- PyClient has no `session[token]`, therefore it provides a token to be sent to a room.

- PyClient has no `session[token]`, therefore it provides a token to be sent to
a room.

### exit

## Scene

###
###
19 changes: 14 additions & 5 deletions zndraw/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import multiprocessing as mp
import contextlib
import uuid

from flask import Flask, redirect, render_template, request, session
from flask_socketio import SocketIO, emit, join_room, call
import contextlib
from flask_socketio import SocketIO, call, emit, join_room

app = Flask(__name__)
app.config["SECRET_KEY"] = str(uuid.uuid4())
Expand Down Expand Up @@ -257,7 +256,12 @@ def analysis_schema(data: dict):
else:
try:
# emit to all webclients in the group, if no sid is provided
emit("analysis:schema", data["schema"], include_self=False, to=session["token"])
emit(
"analysis:schema",
data["schema"],
include_self=False,
to=session["token"],
)
except KeyError:
return "No host found."

Expand All @@ -269,7 +273,12 @@ def modifier_schema(data: dict):
else:
try:
# emit to all webclients in the group, if no sid is provided
emit("modifier:schema", data["schema"], include_self=False, to=session["token"])
emit(
"modifier:schema",
data["schema"],
include_self=False,
to=session["token"],
)
except KeyError:
return "No host found."

Expand Down
2 changes: 1 addition & 1 deletion zndraw/modify/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import abc
import enum
import logging
import typing as t
import time
import typing as t

import ase
import numpy as np
Expand Down
2 changes: 1 addition & 1 deletion zndraw/static/UI/json_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,4 @@ function modifier_editor(socket, cache, world) {
}, 1000);
});
});
}
}
46 changes: 29 additions & 17 deletions zndraw/static/World/World.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,23 +225,35 @@ class World {
this.step = loop.step;
this.socket = socket;

this.socket.on("scene:points", function(callback) {
const { points, segments } = this.getLineData();
callback(points);
}.bind(this));

this.socket.on("scene:segments", function(callback) {
const { points, segments } = this.getLineData();
callback(segments);
}.bind(this));

this.socket.on("scene:step", function(callback) {
callback(this.getStep());
}.bind(this));

this.socket.on("selection:get", function(callback) {
callback(this.getSelection());
}.bind(this));
this.socket.on(
"scene:points",
function (callback) {
const { points, segments } = this.getLineData();
callback(points);
}.bind(this),
);

this.socket.on(
"scene:segments",
function (callback) {
const { points, segments } = this.getLineData();
callback(segments);
}.bind(this),
);

this.socket.on(
"scene:step",
function (callback) {
callback(this.getStep());
}.bind(this),
);

this.socket.on(
"selection:get",
function (callback) {
callback(this.getSelection());
}.bind(this),
);
// renderer.render(scene, camera);
}

Expand Down
2 changes: 1 addition & 1 deletion zndraw/static/World/systems/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ class Selection {
const { points, segments } = this.world.getLineData();
console.log(new Date().toISOString(), "running modifier");
this.socket.emit("modifier:run", {
params: {"method": {"method": "Delete"}},
params: { method: { method: "Delete" } },
url: window.location.href,
});
// particlesGroup.click();
Expand Down
28 changes: 17 additions & 11 deletions zndraw/static/pycom/Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,17 @@ class Cache {
).innerHTML = `${slider.value} / ${slider.max}`;
});

this._socket.on("atoms:download", function(ids, callback) {
// send all atoms at once
const data = {};
ids.forEach((x) => {
data[x] = this._cache[x];
});
callback(data);
}.bind(this));
this._socket.on(
"atoms:download",
function (ids, callback) {
// send all atoms at once
const data = {};
ids.forEach((x) => {
data[x] = this._cache[x];
});
callback(data);
}.bind(this),
);

this._socket.on("atoms:clear", (start_index) => {
// remove everything from the cache starting from start_index
Expand All @@ -196,9 +199,12 @@ class Cache {
});
});

this._socket.on("atoms:length", function(callback) {
callback(this.get_length());
}.bind(this));
this._socket.on(
"atoms:length",
function (callback) {
callback(this.get_length());
}.bind(this),
);
}

get(id) {
Expand Down
4 changes: 2 additions & 2 deletions zndraw/view.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import logging
import multiprocessing as mp
import webbrowser

from zndraw.app import app, io
from zndraw.utils import ZnDrawLoggingHandler
from zndraw.zndraw import ZnDrawDefault, FileIO
import multiprocessing as mp
from zndraw.zndraw import FileIO, ZnDrawDefault

try:
import urllib.request
Expand Down
37 changes: 17 additions & 20 deletions zndraw/zndraw.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
import collections.abc
import contextlib
import dataclasses
import pathlib
import threading
import time
import typing as t
from io import StringIO
import importlib

import ase
import ase.io
import flask_socketio
import networkx as nx
import numpy as np
import socketio
import tqdm
import znh5md

from zndraw.bonds import ASEComputeBonds
from zndraw.data import atoms_from_json, atoms_to_json
from zndraw.select import get_selection_class
from zndraw.analyse import get_analysis_class
from zndraw.utils import ZnDrawLoggingHandler, get_port
from zndraw.data import atoms_from_json, atoms_to_json
from zndraw.draw import Geometry
from zndraw.modify import get_modify_class
from zndraw.select import get_selection_class
from zndraw.settings import GlobalConfig
from zndraw.draw import Geometry
from zndraw.utils import ZnDrawLoggingHandler


def _await_answer(socket, channel, data=None, timeout=5):
Expand Down Expand Up @@ -125,7 +119,7 @@ def __delitem__(self, index):
def insert(self, index, value):
"""Insert atoms before index"""
self.socket.emit("atoms:insert", {index: atoms_to_json(value)})

def append(self, value: ase.Atoms) -> None:
"""Append atoms to the end of the list"""
self[len(self)] = value
Expand Down Expand Up @@ -179,7 +173,7 @@ def segments(self) -> np.ndarray:
@property
def step(self) -> int:
if self._target_sid is not None:
step= int(self.socket.call("scene:step", {"sid": self._target_sid}))
step = int(self.socket.call("scene:step", {"sid": self._target_sid}))
else:
step = int(self.socket.call("scene:step", {}))
return step
Expand Down Expand Up @@ -262,18 +256,19 @@ def analysis_schema(self):
cls = get_analysis_class(config.get_analysis_methods())

self.socket.emit(
"analysis:schema",
{"schema": cls.model_json_schema_from_atoms(self[0]), "sid": self._target_sid},
)

"analysis:schema",
{
"schema": cls.model_json_schema_from_atoms(self[0]),
"sid": self._target_sid,
},
)

def modifier_schema(self):
config = GlobalConfig.load()
cls = get_modify_class(config.get_modify_methods())
data = {"schema": cls.model_json_schema(), "sid": self._target_sid}
self.socket.emit("modifier:schema", data)


def selection_schema(self):
config = GlobalConfig.load()
cls = get_selection_class(config.get_selection_methods())
Expand Down Expand Up @@ -343,16 +338,18 @@ def analysis_run(self, data):
except ValueError as err:
print(err)


@dataclasses.dataclass
class ZnDraw(ZnDrawBase):
"""ZnDraw client.
Attributes
----------
display_new : bool
Display new atoms in the webclient, when they are added.
"""

jupyter: bool = False
display_new: bool = True

Expand Down Expand Up @@ -433,7 +430,7 @@ def log(self, message: str) -> None:

def get_logging_handler(self) -> ZnDrawLoggingHandler:
return ZnDrawLoggingHandler(self.socket)

def __setitem__(self, index, value):
super().__setitem__(index, value)
if self.display_new:
Expand Down

0 comments on commit b4fbf31

Please sign in to comment.