local_visualizer.local_visualizer module

Simple api to visualize the plots in a script.


  • When moving from an IPython notebook to a script, we lose the diagnostics
    of visualizing pandas as tables and matplotlib plots.
  • LocalViz starts a local http server and creates a html file to
    which pandas tables and matplotlib plots can be sent over.
  • The html file is dynamically updated for long running scripts.


Sample Usage:

import logging, sys, numpy as np, pandas as pd, matplotlib.pyplot as plt
import local_visualizer

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

# Create the local visualizer instance
lviz = local_visualizer.LocalViz(html_file='lviz_test.html', port=9112)
# INFO:root:Starting background server at: http://localhost:9112/.
# INFO:local_visualizer:Click: http://carpediem:9112/lviz_test.html or http://localhost:9112/lviz_test.html # noqa

# Create plots which will be streamed to the html file.
lviz.h3('Matplotlib :o')
    'Wrap your plots in the figure context manager which takes '
    'in the kwargs of plt.figure and returns a plt.figure object.',

with lviz.figure(figsize=(10, 8)) as fig:
    x = np.linspace(-10, 10, 1000)
    plt.plot(x, np.sin(x))
    plt.title('Sine test')


# Visualize pandas dataframes as tables.
lviz.h3('Pandas dataframes')

df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df = pd.concat(
    [df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],


This starts a HTTPServer and creates a html file which is dynamically updated each time lviz is called. See https://i.imgur.com/jjwvAX2.png for the output of the above commands.

local_visualizer.local_visualizer.HEADER_LEVELS = [1, 2, 3, 4, 5]

The different HTML header levels.

class local_visualizer.local_visualizer.HtmlGenerator(output_fl=None)

Bases: object

A class which updates a html file and exposes API for the same.

The class also exposes the methods h1, h2, …, h6 for writing headers.


Inserts a break line in the html file.

figure(*args, **kwds)

Context manager as a stand it replacement for plt.figure.

Example usage:

with lviz.figure(figsize=(10, 10)) as fig:
    plt.plot(x, y)
    plt.title('This is a title')
header(text, level=4)

Creates a header line of given level.

  • text (str) – The html header text.
  • level (int) – The level of the html header.

Inserts a horizontal line wrapped in blank lines in the html file.


Writes a paragraph tagged text.

Parameters:text (str) – The html paragraph text.

Appends the text or a pandas df to the output file.

Parameters:text_or_df (str or pandas.DataFrame) – The string or the pandas dataframe to be written to file.
class local_visualizer.local_visualizer.LocalViz(lazy=False, html_file=None, run_server=True, port=9111)

Bases: object

API for creating a html visualizer for python scripts.

All the public methods of HtmlGenerator are also exposed by this class.

See module docstring for usage.

  • html_file (str or NoneType) – Path to the html file to write to. If the file exists already it will be overwritten. If None is passed in, the class will create a temp file.
  • run_server (bool) – Whether the server should started in the background.
  • port (int) – The port at which the server is to be started.
  • _html_gen (HtmlGenerator) – A container for the html generation.
  • is_started (bool) – Has the start been called.
close(*args, **kwargs)

Writes the closing html tags to the html file.

del_html(*args, **kwargs)

Deletes the generated html file.


Mutates self.html_file.

inform_cleanup(*args, **kwargs)

Informs the user which html file to delete at the end.


Creates the html file and possibly starts the bgd http server.


  • self.html_file
  • self._html_gen
  • self.is_started

Deletes a list of files if they exist.

Parameters:files (list of str) – A list of file paths.
local_visualizer.local_visualizer.run_bgd_server(port, host='localhost')

Creates a simple http server in a daemon thread.

  • host (str) – The host id where the server has to be started, ex. 'localhost'.
  • port (int) – The port where the local server should serve.

A daemon thread running a simple http server in the background.




Decorater for LocalViz methods to ensure the instance has been started.