Skip to main content

an implementation of visjs from networkx with export svg

Project description

pvsvg

Introduction

a python wrapper of the vis.js network visualization library with an additional ability to export static SVG.

Inspired by the excellent python library pyvis but I required a function to export the resulting network as a static SVG.

The code for exporting the SVG is taken from justinharrell/vis-svg.

Installation

You can install this via pip:

pip install pvsvg

Usage

This library exposes a single class ( Network ) which takes as input a networkx Graph.

All the configuration for node and edge level attributes (like color, size, mass) are passed transparently to vis.js so you can configure your resulting network via the attributes of your networkx nodes and edges respectively.

You can see node level configuration and edge level configuration on the vis.js documentation.

Exporting as SVG

The resulting HTML file will have some configuration buttons built-in. One of these is the export-svg button which will generate the static SVG and write it to a file (network.svg)

Keep in mind that it will write the SVG of whatever is within the Canvas element at the time. Essentially the view you have within the boundaries of the Canvas is exactly what will be written to the SVG.

Note: Currently the node borders are not being written to the SVG. Besides that everything else should appear.

Example

from pvsvg import Network
import networkx as nx

# generate some random graph
g = nx.fast_gnp_random_graph(15, 0.2)

# set node level attributes
for n in g.nodes(data=True):
    id = n[0]
    if id % 2 == 0:
        n[1]["color"] = "red"
    else:
        n[1]["color"] = "blue"

    if id % 3 == 0:
        n[1]["shape"] = "square"
    else:
        n[1]["shape"] = "dot"

    n[1]["label"] = f"Node {id}"
    n[1]["title"] = f"Node {id} has color {n[1]['color']} and shape {n[1]['shape']}"
    n[1]["size"] = (id + 1)

# set edge level attributes
for e in g.edges(data=True):
    e[2]["color"] = {
        "color": "black",
        "inherit": False,
    }

# Instantiate the network
n = Network(g, width=1200)

# Draw the network as an html
n.draw("test.html")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pvsvg-0.1.3.tar.gz (405.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pvsvg-0.1.3-py3-none-any.whl (409.5 kB view details)

Uploaded Python 3

File details

Details for the file pvsvg-0.1.3.tar.gz.

File metadata

  • Download URL: pvsvg-0.1.3.tar.gz
  • Upload date:
  • Size: 405.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.4 Darwin/23.0.0

File hashes

Hashes for pvsvg-0.1.3.tar.gz
Algorithm Hash digest
SHA256 80b39239923b52bccc470c78a521438ace12b9d6afb114b05240e346ddcab024
MD5 2a52535f54c60df8eb9a308f04b92abd
BLAKE2b-256 0f4ad8ee1944828c58ee4de87667e23bcc38f3fcb9d944d55993c0ba9fb392b2

See more details on using hashes here.

File details

Details for the file pvsvg-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pvsvg-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 409.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.4 Darwin/23.0.0

File hashes

Hashes for pvsvg-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bad0069c1d00fb1ee839420347ede03c363c453c22dd560bfac574b6ef1fbf05
MD5 9f2cf71570e68039771d886b0fc66796
BLAKE2b-256 bcf77d53b35d3b9bf1d3425effdbf00b057959af6ae4aa5d6f136309082b0139

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page