Interactive module

This module provides the necessary functions to start up a local HTTP server and open an interactive d3-visualization of a network.

class netwulf.interactive.NetwulfHTTPRequestHandler(*args, directory=None, **kwargs)[source]

Bases: http.server.SimpleHTTPRequestHandler

A custom handler class adapted from https://stackoverflow.com/questions/6204029/extending-basehttprequesthandler-getting-the-posted-data and https://blog.anvileight.com/posts/simple-python-http-server/#do-post

do_POST()[source]
log_message(format, *args)[source]

Log an arbitrary message.

This is used by all other logging functions. Override it if you have specific logging wishes.

The first argument, FORMAT, is a format string for the message to be logged. If the format string contains any % escapes requiring parameters, they should be specified as subsequent arguments (it’s just like printf!).

The client ip and current date/time are prefixed to every message.

class netwulf.interactive.NetwulfHTTPServer(server_address, handler, subjson, verbose=False)[source]

Bases: http.server.HTTPServer

Custom netwulf server class adapted from https://stackoverflow.com/questions/268629/how-to-stop-basehttpserver-serve-forever-in-a-basehttprequesthandler-subclass

end_requested = False
posted_config = None
posted_image_base64 = None
posted_network_properties = None
run()[source]
serve_forever()[source]

Handle one request at a time until doomsday.

stop_this()[source]
netwulf.interactive.mkdirp_customdir(directory=None)[source]

simulate mkdir -p functionality

netwulf.interactive.prepare_visualization_directory()[source]

Move all files from the netwulf/js directory to ~/.netwulf

netwulf.interactive.visualize(network, port=9853, verbose=False, config=None, plot_in_cell_below=True, is_test=False)[source]

Visualize a network interactively using Ulf Aslak’s d3 web app. Saves the network as json, saves the passed config and runs a local HTTP server which then runs the web app.

Parameters:
  • network (networkx.Graph or networkx.DiGraph or node-link dictionary) – The network to visualize
  • port (int, default : 9853) – The port at which to run the server locally.
  • verbose (bool, default : False) – Be chatty.
  • config (dict, default : None,) –

    In the default configuration, each key-value-pair will be overwritten with the key-value-pair provided in config. The default configuration is

    default_config = {
        # Input/output
        ‘zoom’: 1,
        # Physics
        ‘node_charge’: -45,
        ‘node_gravity’: 0.1,
        ‘link_distance’: 15,
        ‘link_distance_variation’: 0,
        ‘node_collision’: True,
        ‘wiggle_nodes’: False,
        ‘freeze_nodes’: False,
        # Nodes
        ‘node_fill_color’: ‘#79aaa0’,
        ‘node_stroke_color’: ‘#555555’,
        ‘node_label_color’: ‘#000000’,
        ‘display_node_labels’: False,
        ‘scale_node_size_by_strength’: False,
        ‘node_size’: 5,
        ‘node_stroke_width’: 1,
        ‘node_size_variation’: 0.5,
        # Links
        ‘link_color’: ‘#7c7c7c’,
        ‘link_width’: 2,
        ‘link_alpha’: 0.5,
        ‘link_width_variation’: 0.5,
        # Thresholding
        ‘display_singleton_nodes’: True,
        ‘min_link_weight_percentile’: 0,
        ‘max_link_weight_percentile’: 1
    }
    

    When started from a Jupyter notebook, this will show a reproduced matplotlib figure of the stylized network in a cell below. Only works if verbose = False.

  • is_test (bool, default : False) – If True, the interactive environment will post its visualization to Python automatically after 5 seconds.
Returns:

  • network_properties (dict) – contains all necessary information to redraw the figure which was created in the interactive visualization
  • config (dict) – contains all configurational values of the interactive visualization