local_visualizer.local_visualizer module

Simple api to visualize the plots in a script.

Motivation

  • 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.

Usage

Sample Usage:

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

plt.style.use('fivethirtyeight')
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')
lviz.p(
    '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')

lviz.hr()

# 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'))],
    axis=1,
)
lviz.write(df)
lviz.close()

Output

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.

br()

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.

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

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

p(text)

Writes a paragraph tagged text.

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

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.

close(*args, **kwargs)

Writes the closing html tags to the html file.

del_html(*args, **kwargs)

Deletes the generated html file.

Note

Mutates self.html_file.

inform_cleanup(*args, **kwargs)

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

start()

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

Mutates

  • self.html_file
  • self._html_gen
  • self.is_started
local_visualizer.local_visualizer.delete_files_silently(files)

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.

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

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

Type:

threading.Thread

local_visualizer.local_visualizer.validate_lviz_started(method)

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