HTTP Server¶
To use the library normally (i.e. not through Jupyter), you will need to set up a local server for displaying the interactive network. The library comes with all the tools needed to do this:
from algorithmx import http_server
# Create a new HTTP server on port 5050
server = http_server(port=5050)
# Create a new canvas
canvas = server.canvas()
def start():
# Use the library normally, for example:
canvas.nodes([1, 2]).add()
canvas.edge((1, 2)).add()
canvas.pause(1)
canvas.node(1).highlight().size('1.5x').pause(0.5)
canvas.edge((1, 2)).traverse('blue')
# A 'start' message is sent by the client whenever the
# user clicks the start or restart button
canvas.onmessage('start', start)
# Start the server, blocking all further execution on
# the current thread. Use 'ctrl-c' to exit the script.
server.start()
After running this code, open a browser and go to http://localhost:5050/
to see the
network. Note that a websocket is open on the next incremental port (by default 5051).
The library provides a simple HTML interface with buttons for starting, stopping and restarting the simulation. If you wish to customize this further, you can tell the server to open a different HTML file.
server = algorithmx.http_server(file='my/custom/interface.html')
Use the provided HTML file as a guide for creating your own.
-
algorithmx.
http_server
(file: str = None, host: str = 'localhost', port: int = 5050) → algorithmx.server.CanvasServer.CanvasServer[source]¶ Creates a new HTTP server for displaying the network, using WebSockets to transmit data. The server will only start once its
start()
method is called. After the server has started, the network can be viewed by opening a browser and navigating to the addresshttp://localhost:5050/
(change the port as necessary).- File
(Optional) The path to the HTML file which the server should display, relative to the current runtime directory. If unspecified, the default HTML file will be used. When creating a custom HTML interface, use the default file as a guide.
- Port
(Optional) The port on which the server should start, defaulting to to 5050. Note that the next port (by default 5051) will also be used to transmit data through WebSockets.
-
class
algorithmx.server.
CanvasServer
(file: Optional[str], host: str, port: int)[source]¶ A local HTTP server using WebSockets to transmit data.
-
start
()[source]¶ Starts the server on the current threat, blocking all further execution until the server shuts down.
-
shutdown
()[source]¶ Shuts down the server. This must be called on a different thread to the one used to start the server.
-
canvas
(name: Optional[str] = None) → algorithmx.api.Canvas.Canvas[source]¶ Returns an
Canvas
interface, which will dispatch and receive events through a WebSocket connected to the server.- Name
(Optional) The name of the canvas. By default, each server will only render one canvas, and so this argument has no affect. However, if you wish to design a custom UI with more than one canvas per page, you can use this to differentiate between them.
-