Month: November 2020

Displaying a network chart graph diagram on a Django site

There is often a case for improving the understanding of things with visualisations.
Python provides a number of libraries to create great visualisations - however they often are focused more on a data science approach - scripts and jupyter notebooks.

What we want is the visualisations to be easily accessible through a django website, that is what I will be showing in this post with a specific focus on network diagrams.

What Visualisation Package are we using for the Network Graph

I have tried to find a few packages for creating a network graph:

Comparison

I will be using graphviz and networkx for very simple rudementary network graphs. You can make them look good but it is strenuous effort.

I will let you try networkx with plotly and igraph on your own.

Graphviz

Graphviz was very simple output and input. Not too much fuss and can render in many formats. To create a simple graph displayed on the frontend as an SVG:

from graphviz import Graph

g = Graph(
    'G',
    format='svg',
    engine='twopi',
)

g.node('root', shape='rectangle', width='1.5')
g.node('red')
g.node('blue')

g.edge('root', 'red', label='to_red')
g.edge('root', 'blue', label='to_blue')

context_data['my_chart'] = g.pipe().decode('utf-8')

Display on frontend:

{{ my_chart | safe }}

The image output:

graphviz-simple-radial-django

NetworkX

NetworkX is not primarily a graph drawing package but basic drawing with Matplotlib as well as an interface to use the open source Graphviz software package are included.

The networkx plot is drawn using matplotlib (it can also use graphviz) to draw.

    import matplotlib.pyplot as plt
    import networkx as nx
    import io

    G = nx.Graph()

    # rectanle width 1.5
    G.add_node('root')
    G.add_node('red')
    G.add_node('blue')

    # label: to_red
    G.add_edge('root', 'red')
    # label: to_blue
    G.add_edge('root', 'blue')

    nx.draw(G)
    buf = io.BytesIO()
    plt.savefig(buf, format='svg', bbox_inches='tight')
    image_bytes = buf.getvalue().decode('utf-8')
    buf.close()
    plt.close()

    context_data['my_chart'] = image_bytes

Display on frontend:

{{ my_chart | safe }}

The image output:

networkx-simple-django-network

No annotations...

Make sure you close the plot otherwise it will cause issues, matplotlib is not thread safe...

Important:

  • nx.draw(G) - draws with no labels
  • nx.draw_networkx(G) - Draws with labels

networkx-with-labels

Sources

Installing and Running a Full Node with Bitcoin Core Daemon on Ubuntu 20.04

In this guide I will be showing the steps I used to get a bitcion full node running.
It is based mainly on the bitcoin core full node guide however I also used some info from the Mastering Bitcoin book

Prerequisites

In order to run a full node your computer needs to meet certain performance metrics.

  • Desktop or laptop hardware running recent versions of Windows, Mac OS X, or Linux.
  • 350 gigabytes of free disk space, accessible at a minimum read/write speed of 100 MB/s.
  • 2 gigabytes of memory (RAM)
  • A broadband Internet connection with upload speeds of at least 400 kilobits (50 kilobytes) per second
  • An unmetered connection - download usage is around 20 gigabytes a month, plus around an additional 340 gigabytes the first time you start your node.
  • 6 hours a day that your full node can be left running.

If you meet these requirements you can continue

Install Ubuntu and Bitcoin Core

Install a fresh GNU/linux ubuntu OS instance on your computer, downloadable from ubuntu/downloads

Important to install a fresh OS and verify the hash to ensure it has not been altered - as we are dealing with bitcon and must ensure we are not susceptible to attackers

Once installed, update and upgrade the system.

sudo apt update
sudo apt upgrade
sudo wget https://bitcoin.org/bin/bitcoin-core-0.20.1/bitcoin-0.20.1-x86_64-linux-gnu.tar.gz
sha256sum bitcoin-0.20.1-x86_64-linux-gnu.tar.gz
# ensure the hash matches the verification signatures
sudo tar xzf bitcoin-0.20.1-x86_64-linux-gnu.tar.gz
# Install the binaries in /usr/local/bin
sudo install -m 0755 -o root -g root -t /usr/local/bin/ bitcoin-0.20.1/bin/*

Now at this point you can decide to go the User interface route

I will be using the Bitcoin core daemon...

Running Bitcoin Core Daemon

Start the daemon

bitcoind -daemon
...Bitcoin Core starting

To interact wih the daemon we use bitcoin-cli:

There are some useful commands, a complete list can be found at bitcoin developer reference

Once started the daemon will start downloading the blockchain, which is over 300Gb now. So ensure to change the data directory to a storage device that is large enough.

Get the current block count by doing:

bitcoin-cli getblockcount
# 194044

The current block count can be found at https://blockchain.info/q/getblockcount
December 14, 2020 10:51 SAST it is 661306

Changing the data directory

By default on linux, bitcoind will put data in:

~/.bitcoin

To change this create a file called ~.bitcion/bitcoin.conf and add the line:

datadir=/your/path

Ensure the node starts at boot time

crontab -e

Scroll to the bottom and add:

@reboot bitcoind -daemon

Networking Setup

The next step is ensuing that your node is accessible to the internet, so let us look at the bitcoin full node guide