-
Notifications
You must be signed in to change notification settings - Fork 2
/
phaseportrait-launcher.py
249 lines (192 loc) · 8.72 KB
/
phaseportrait-launcher.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
try:
from phaseportrait import PhasePortrait2D, PhasePotrait3D
except (ImportError, ModuleNotFoundError):
from phaseportrait_local.phaseportrait import PhasePortrait2D, PhasePortrait3D
import io
import json
import mimetypes
import os
import traceback
from pathlib import Path
import tornado
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.websocket
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib.backends.backend_webagg import \
FigureManagerWebAgg, new_figure_manager_given_figure
files_path = '/'.join(__file__.split('\\')[:-1])
class Logger:
def __init__(self):
# self.log_file = open("log.txt", 'a')
...
def __call__(self, message):
with open("log.txt", 'a') as log_file:
log_file.write(repr(message)+'\n')
# log_file.write(traceback.format_exc(message) + '\n')
traceback.print_exc(file=log_file)
def __del__(self):
# self.log_file.close()
...
class PhasePortraitServer(tornado.web.Application):
logger = Logger()
class Download(tornado.web.RequestHandler):
"""
Handles downloading of the figure in various file formats.
"""
def get(self, fmt):
manager = self.application.manager
self.set_header(
'Content-Type', mimetypes.types_map.get(fmt, 'binary'))
buff = io.BytesIO()
manager.canvas.figure.savefig(buff, format=fmt)
self.write(buff.getvalue())
class PlotWebSocket(tornado.websocket.WebSocketHandler):
"""
A websocket for interactive communication between the plot in
the browser and the server.
In addition to the methods required by tornado, it is required to
have two callback methods:
- ``send_json(json_content)`` is called by matplotlib when
it needs to send json to the browser. `json_content` is
a JSON tree (Python dictionary), and it is the responsibility
of this implementation to encode it as a string to send over
the socket.
- ``send_binary(blob)`` is called to send binary image data
to the browser.
"""
supports_binary = True
_prev_ = None
logger = Logger()
def open(self):
# Register the websocket with the FigureManager.
manager = self.application.manager
manager.add_web_socket(self)
if hasattr(self, 'set_nodelay'):
self.set_nodelay(True)
def on_close(self):
# When the socket is closed, deregister the websocket with
# the FigureManager.
manager = self.application.manager
manager.remove_web_socket(self)
def on_message(self, message):
# The 'supports_binary' message is relevant to the
# websocket itself. The other messages get passed along
# to matplotlib as-is.
# Every message has a "type" and a "figure_id".
message = json.loads(message)
if message['type'] == 'supports_binary':
self.supports_binary = message['value']
else:
manager = self.application.manager
if message['type'] == 'button_release' or \
(self._prev_ == 'home'):
ax = self.application.phaseportrait.ax
x_lim = ax.get_xlim()
y_lim = ax.get_ylim()
ax.cla()
self.application.phaseportrait.Range = [x_lim, y_lim]
try:
self.application.phaseportrait.plot()
except Exception as e:
print(e, flush=True, end='')
self.logger(e)
self._prev_ = message.get('type', None)
try:
manager.handle_json(message)
except Exception as e:
self.logger(e)
def send_json(self, content):
self.write_message(json.dumps(content))
def send_binary(self, blob):
if self.supports_binary:
self.write_message(blob, binary=True)
else:
data_uri = "data:image/png;base64,{0}".format(
blob.encode('base64').replace('\n', ''))
self.write_message(data_uri)
class PPSocket(tornado.websocket.WebSocketHandler):
logger = Logger()
last_message = None
def open(self):
if hasattr(self, 'set_nodelay'):
self.set_nodelay(True)
def on_close(self):
pass
def check_message_diference(self, message, check_list):
if self.last_message is None:
return True
for check in check_list:
if self.last_message[check] != message[check]:
return True
return False
def on_message(self, message):
# Log message for debuggin intentions
self.logger(message)
message = json.loads(message)
try:
# First check if the message needs other phaseportrait object type than previous message.
# If so, execute start_phaseportrait in order to create a new figure
if self.check_message_diference(message, ["dimension", "dF", "phaseportrait_object_type"]):
# If a phaseportrait is already created close figure and delete it
if hasattr(self.application, "phaseportrait"):
plt.close(self.application.phaseportrait.fig)
del self.application.phaseportrait
result = self.application.start_phaseportrait(message=message)
# If not new phaseportrait object type is needed handle message internally
else:
# Try to handle the message via internal manager
result = self.application.phaseportrait.manager.handle_json(message)
# If and error occur, start all over again
if result == 1:
result = self.application.start_phaseportrait(message=message)
# If result is None or there was not problems then figure id is assign
if (result is None) or (result == 0):
result = self.application.phaseportrait.fig.number
except Exception as e:
self.write_error(str(e))
# Send the appropiate message and save this message for later comparison
self.write_message(str(result))
self.last_message = message
def start_phaseportrait(self, *, message=None):
try:
dF = lambda x,y: (y,-x)
Range = [[0,1], [0,1]]
if message:
pp_object = globals()[message["phaseportrait_object_type"]]
if message["dimension"] == 3:
dF = lambda x,y,z: (y,-x,-z)
Range = [[-1,1], [-1,1], [-1,1]]
else:
pp_object = PhasePortrait2D
self.phaseportrait = pp_object(dF, Range)
self.figure = self.phaseportrait.fig
self.manager = new_figure_manager_given_figure(self.figure.number, self.figure)
if message is not None:
result = self.phaseportrait.manager.handle_json(message)
else:
self.phaseportrait.plot()
except Exception as e:
self.logger(e)
return self.figure.number
def __init__(self):
self.FigId = self.start_phaseportrait()
super().__init__([
# Sends images and events to the browser, and receives
# events from the browser
('/ws', self.PlotWebSocket),
# Handles the downloading (i.e., saving) of static images
(r'/download.([a-z0-9.]+)', self.Download),
# For comunication with PhasePortrait
('/pp', self.PPSocket)
])
if __name__ == '__main__':
# mpl.use("WebAgg")
mpl.rcParams["backend"] = "WebAgg"
application = PhasePortraitServer()
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8080)
print(f"localhost:8080, {application.FigId}", flush=True, end='')
tornado.ioloop.IOLoop.current().start()