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:

import algorithmx

# Create a new HTTP server
server = algorithmx.http_server(port=5050)
# Create a CanvasSelection interface
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)).animate('traverse').color('blue')

# Call the function above when the client broadcasts a 'start' message
# (which will happen when the user clicks the start or restart button)
canvas.listen('start', start)

# Start the server, blocking all further execution on the current thread
# You can press 'CTRL-C' to exit the script
server.start()

After running this code, open a browser and go to the address http://localhost:5050/ to see the network. 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, and configure the port:

server = algorithmx.http_server(file='my/custom/interface.html', port=8090)

Use the provided HTML file as a guide for creating your own.

algorithmx.http_server(file: str = None, port: int = 5050) → algorithmx.server.Server.Server[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 address http://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.Server(file: 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: str = 'output') → algorithmx.graphics.CanvasSelection.CanvasSelection[source]

Creates a new CanvasSelection which will dispatch and receive events through a WebSocket connected to the server.

Parameters

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 interface with more than one canvas per page, you can use this to differentiate between them.