Skip to content

Commit a023bf9

Browse files
committed
Add get_server_address() and simplify starting the ete server.
We changed start_server() so that it automatically tracks the created server in g_threads (unless we set track=False). It also sets the compression (with the compress argument) to use when communicating. So we do not have to do it somewhere else. I think it makes sense here. Finally, with the new get_server_address() which returns (host, port), we can simplify quite a bit of code, and make it look independent of the kind of server that we use (wsgiref.simple_server, cheroot, whatever).
1 parent 12addab commit a023bf9

File tree

3 files changed

+36
-31
lines changed

3 files changed

+36
-31
lines changed

ete4/core/tree.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ cdef class Tree:
10831083

10841084
# Add tree and start a server.
10851085
name = explorer.add_tree(self, layouts=layouts)
1086-
thread, server = explorer.start_server()
1086+
thread_server = explorer.start_server(track=False)
10871087

10881088
# Use selenium to make a screenshot.
10891089
w = w or 2560 # width
@@ -1100,7 +1100,7 @@ cdef class Tree:
11001100

11011101
driver = webdriver.Chrome(options=options)
11021102

1103-
host, port = server.bind_addr
1103+
host, port = explorer.get_server_address()
11041104
driver.get(f'http://{host}:{port}/static/gui.html?tree={name}')
11051105

11061106
time.sleep(2) # wait, kind of a hack
@@ -1110,7 +1110,7 @@ cdef class Tree:
11101110
driver.quit()
11111111

11121112
# Stop the server (and close its thread), and remove the tree.
1113-
explorer.stop_server((thread, server), remove_trees=False)
1113+
explorer.stop_server(thread_server, remove_trees=False)
11141114
explorer.remove_tree(name)
11151115

11161116
def copy(self, method="cpickle"):

ete4/smartview/explorer.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -659,27 +659,29 @@ def explore(tree, name=None, layouts=None,
659659
"""Run the web server, add tree and open a browser to visualize it."""
660660
add_tree(tree, name, layouts, kwargs)
661661

662-
if compress is not None:
663-
g_config['compress'] = compress # global configuration
664-
665662
# Launch the thread with the http server (if not already running).
666663
if 'server' not in g_threads:
667-
thread, server = start_server(host, port, verbose, keep_server,
668-
server_args)
669-
g_threads['server'] = (thread, server)
670-
host, port = server.bind_addr # port may have changed
671-
print(f'Explorer now available at http://{host}:{port}')
664+
print('Creating new server.')
665+
start_server(host, port, verbose, compress, keep_server, server_args)
672666
else:
673-
_, server = g_threads['server']
674-
host, port = server.bind_addr
675-
print(f'Existing explorer available at http://{host}:{port}')
667+
print('Using existing server.')
668+
669+
host, port = get_server_address()
670+
print(f'Explorer available at http://{host}:{port}')
676671

677672
if open_browser:
678-
_, server = g_threads['server']
679-
host, port = server.bind_addr
680673
open_browser_window(host, port)
681674

682675

676+
def get_server_address():
677+
"""Return (host, port) where the server is listening."""
678+
if 'server' in g_threads:
679+
_, server = g_threads['server']
680+
return server.bind_addr # (host, port)
681+
else:
682+
return None, None
683+
684+
683685
def add_tree(tree, name=None, layouts=None, extra_style=None):
684686
"""Add tree, layouts, etc to the global variables, and return its name."""
685687
name = name or make_name() # in case we didn't receive one
@@ -705,18 +707,21 @@ def remove_tree(name):
705707
g_layouts.pop(name)
706708

707709

708-
def start_server(host='127.0.0.1', port=None, verbose=False, keep_server=False,
709-
server_args=None):
710+
def start_server(host='127.0.0.1', port=None, verbose=False, compress=None,
711+
keep_server=False, server_args=None, track=True):
710712
"""Create a thread running the web server and return it and the server."""
711-
server_args = server_args or {} # extra server arguments
712-
server_args.setdefault('numthreads', 100)
713-
714713
port = port or get_next_available_port(host)
715714
assert port, 'could not find any port available'
716715

717716
if verbose:
718717
default_app().install(log_requests)
719718

719+
if compress is not None:
720+
g_config['compress'] = compress # global configuration
721+
722+
server_args = server_args or {} # extra server arguments
723+
server_args.setdefault('numthreads', 100)
724+
720725
server = Server((host, port), default_app(), **server_args)
721726

722727
thread = Thread(
@@ -725,6 +730,9 @@ def start_server(host='127.0.0.1', port=None, verbose=False, keep_server=False,
725730

726731
thread.start()
727732

733+
if track: # we normally want to keep track of the server in g_threads
734+
g_threads['server'] = (thread, server)
735+
728736
return thread, server
729737

730738

@@ -810,15 +818,12 @@ def stop_server(thread_server=None, remove_trees=True):
810818
g_trees[name] = t
811819
g_layouts[name] = [BASIC_LAYOUT]
812820

813-
# Set the global config options.
814-
g_config['compress'] = args.compress
815-
816821
# Launch the http server in a thread and open the browser.
817-
_, server = start_server('127.0.0.1', args.port, args.verbose)
818-
port = server.bind_addr[1]
819-
open_browser_window(port=port)
822+
start_server('127.0.0.1', args.port, args.verbose, args.compress)
823+
host, port = get_server_address()
824+
open_browser_window(host, port)
820825

821-
print(f'Explorer available at http://127.0.0.1:{port}')
826+
print(f'Explorer available at http://{host}:{port}')
822827
input('Press enter to stop the server and finish.\n')
823828
except (FileNotFoundError, newick.NewickError, ValueError) as e:
824829
sys.exit(f'Error using tree from {args.FILE}: {e}')

examples/explorer/ete_server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import ete4.smartview.explorer as ex # to get all the server functions
1111

1212

13-
# Add NameLayout, a simple example of a layout, that we will use in /load_custom.
13+
# Add NameLayout, an example layout that we will use in /load_custom.
1414

1515
def draw_node(node):
1616
yield TextFace(node.name)
@@ -45,9 +45,9 @@ def callback():
4545

4646
# Run the server and show where it is.
4747

48-
_, server = ex.start_server(verbose=True) # just to show what is going on
48+
ex.start_server(verbose=True) # just to show what is going on
4949

50-
host, port = server.bind_addr
50+
host, port = ex.get_server_address()
5151
print(f'Explorer available at http://{host}:{port}')
5252

5353
print('Press enter to stop the server and finish.')

0 commit comments

Comments
 (0)