10000 Allow specifying a port in WebAgg example. · matplotlib/matplotlib@d17fe2a · GitHub
[go: up one dir, main page]

Skip to content

Commit d17fe2a

Browse files
committed
Allow specifying a port in WebAgg example.
1 parent 12d3c8e commit d17fe2a

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

examples/user_interfaces/embedding_webagg_sgskip.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
The framework being used must support web sockets.
1212
"""
1313

14+
import argparse
1415
import io
1516
import json
1617
import mimetypes
1718
from pathlib import Path
19+
import socket
1820

1921
try:
2022
import tornado
@@ -238,13 +240,23 @@ def __init__(self, figure):
238240

239241

240242
if __name__ == "__main__":
243+
parser = argparse.ArgumentParser()
244+
parser.add_argument('-p', '--port', type=int, default=8080,
245+
help='Port to listen on (0 for a random port).')
246+
args = parser.parse_args()
247+
241248
figure = create_figure()
242249
application = MyApplication(figure)
243250

244251
http_server = tornado.httpserver.HTTPServer(application)
245-
http_server.listen(8080)
246-
247-
print("http://127.0.0.1:8080/")
252+
sockets = tornado.netutil.bind_sockets(args.port, '')
253+
http_server.add_sockets(sockets)
254+
255+
for s in sockets:
256+
addr, port = s.getsockname()[:2]
257+
if s.family is socket.AF_INET6:
258+
addr = f'[{addr}]'
259+
print(f"Listening on http://{addr}:{port}/")
248260
print("Press Ctrl+C to quit")
249261

250262
tornado.ioloop.IOLoop.instance().start()

0 commit comments

Comments
 (0)
0