Prevue™ Documentation

✨ Overview

Prevue™ was designed as a Python flowchart visualizer, but it can also be viewed as a general-purpose flow-chart-making program.  It can be used to express pseudo-code in any programming language.

Prevue™ can take a Python source-code file as input and generate an easy-to-grasp flowchart as an SVG image file.  That image shows the viewer how Python behaves in terms of its conditional branches.  Time is saved, algorithms are corrected sooner, and such corrections might even save lives.

Prevue can help learners visualize the flow-of-control in a Python script.  Experienced pythonistas will find that Prevue™ provides a fast way to analyze deeply nested control constructs.

✨ Features

 s1   Generates an SVG image file from a user-specified Python source code file.  s2   Utilizes color.  Images are scalable, and can be viewed directly       in most browsers, and can also be placed into HTML files.  s3   Supports many basic and intermediate Python features.

✨ A Prevue™ Example

The following Python program generates the famous Fibonacci sequence.  The Python program had its flowchart generated by Prevue™, and that's what you see below.

Take note of the grey rectangle at the top center of the diagram's window.  To the left of the grey rectangle are + and - buttons.  Hit one or the other to to alter the diagram's size.

Note that each element in the Fibonacci sequence is equal to the sum of the two previous elements.

✨ Three Python Sort Functions

I implemented three sort algorithms to use as Prevue™ examples for this article.  Below are three functions -- `bubble sort`, `selection sort`, and `merge sort`.  The object being sorted is a Python list.

For larger lists, `selection sort` can have significantly fewer swaps and be significantly faster than `bubble sort`.  For very large lists, `merge sort` is clearly the fastest of my sort functions.  Python has a built-in list sorting function named `sorted`, which I trust sorts correctly, and so I use it below to help validate each of my three sort functions.

Because I have cairosvg, I can convert a Prevue™-generated .svg file to a .pdf file.  The images shown in this article are PDF images.

As seen above, most control words are colored `dark red`.  However, you will see four control words near the bottom of the `bubble_sort` function that are colored gold.  Each gold word is on a line that was not in the original Python source file.

Prevue™ inserts such lines into the SVG image file -- and not into the Python source file.  These inserted lines help support the Prevue™ diagrams.

In Python, one can terminate an `if` block by using `return`, `continue` or `break`.  But what about the ordinary `if block`?  In that case, Python offers unindenting as the only way to terminate the block.

Prevue™ does not require the use of `#end`, but if it is used, then Prevue™ provides feedback to assist in its correct placement.

🔑 Setting Up Prevue™

The zipped Prevue™ file must be unzipped into a folder that is directly below the home folder.  So, if your name is Alan, then you probably have a folder named /home/Alan.  In that case, you must create a folder named `prevue` that is located just below ~ which is shorthand for your home folder.  The following two Terminal commands can be used for that purpose:

        cd ~
        mkdir prevue

Now, you must get into the prevue folder, move the prevue.zip file to the current folder, and unzip the `prevue.zip` file into the current folder via:

        cd prevue
        mv /path/to/prevue.zip .
        unzip prevue.zip

Because Prevue depends on svgwrite, you must install svgwrite.  To do that, give the following Terminal command.

        pip install svgwrite

If you don't already have `pip`, then you must install that too.  Ask any AI how to do that.

After unzipping, copy pv and pvi.py to your ~/bin folder.  If it doesn't exist, create it via:

        cd ~
        mkdir bin

Now copy pv and pvi.py to your ~/bin folder via:

        cd bin
        cp ../svgwrite/pv .
        cp ../svgwrite/pvi.py .

Now, Prevue is "installed".

Suppose you have a Python script named `planner.py` that you are developing.  Now, suppose you wish to see it flowcharted by Prevue.  You can get into the folder where `planner.py` is located, and give the following command:

        pv planner.py

The file `planner.py.svg` will be generated and put into the same folder as `planner.py`.  To view `planner.py.svg`, load it into a browser or a document viewer such as Okular.

🔑 Prevue™ Adherence Rules:

 s1   Do not use elif.  It is not currently supported.  s2   Three control words (return, continue, break) should not       be placed between an if and a corresponding else,       at least, not at the same level of the if/else pair.  s3   When defining data that spans one or more lines:       Place the first visible character of each line of data       at the current indentation level of your code.  s4   Do not use `\` at end of a line as a line-continuation character.

I wanted to release the first version of Prevue™ as soon as possible, and so I didn't include support for `elif` in version 1.0.  However, it is quite easy to emulate `elif`.  Consider the following.  Below, we see a Python elif block, and in the image below that, we see Python code that works in exactly the same way.  Clearly, elif is nice to have, but it's not necessary.

            if cond1:
                stmt1
            elif cond2:
                stmt2
            elif cond3:
                stmt3
            else:
                stmt4

cond is short for condition
stmt is short for statement

The elif statement is well designed for handling a set of mutually exclusive conditions.  What that means is this:  if any particular condition is true, then each of the other conditions is false.  In other words, if any one condition is true, then all other conditions are excluded from being true -- hence the phrase `mutually exclusive`.  However, just because elif can handle a set of `mutually exclusive` conditions, does not mean that its conditions are `mutually exclusive` each and every time that `elif` is used.

✨ Using Prevue™

The following terminal command is an example of using Prevue™ to make an SVG image file:

        $ pv name.py

The SVG image file can be viewed if loaded into a browser like Firefox.  Note that the Prevue™ executable name was shortened to `pv` for the user's convenience.

An SVG file can require megabytes of memory.  Cairosvg is a Terminal command-line program that can convert an SVG image file to one of several other image file types.  In the command line, you tell Cairosvg which image type you want.  For example:

        $ cairosvg nameA.svg > nameB.pdf

I'm happy that cairosvg can create a PDF image file whose size is a small fraction of the originating SVG file.  To my eyes, the PDF image file type looks just as sharp and colorful.  Many image file types (certainly SVG and PDF) are scalable on a browser.

If you convert SVG regularly to PDF, then you may accrue many huge unwanted SVG files, but you can delete all of them in one go from the current directory via this sweet little Terminal command:

        $ rm *.svg

I use the HTML `embed` tag to insert a .pdf image (or an .svg image) into an `.html` file.  Here is an example of an `embed` tag that you can follow:

    <embed src="fib.pdf" width="830px" height="751px" />

For your convenience, two numbers (one for each of width and height) will be provided as output when you give the Terminal command to run Prevue.  If you employ the two numbers in an HTML embed tag, you are free to adjust them as you wish.

An image file can also be viewed directly in Firefox.  To see it that way, you need to tell Firefox where to find the image file on your computer's hard drive.  This is accomplished by placing text such as the following in the brower's URL field.

        file:///home/richard/prevue/fib.pdf

The same method works for the direct viewing of an .svg image.

Besides browsers, you can also see images by using a `document viewer`.  One that I recommend is `Okular`.

✨ Troubleshooting Prevue™

If the Python3 interpreter raises an error while `py` attempts to process your script, the problem could be due to (1) your script breaks Prevue rules, (2) your script breaks Python syntax rules (3) Prevue has an unknown `bug`.

By using Prevue's trace flag (-t), you might be able to isolate the cause.  From the Terminal, execute a command such as the following:

        pv -t name.py

As each line of `name.py` is being processed, the `-t` flag will cause the current line number to be printed to the standard output of the Terminal.  The last few line numbers shown before the error occurs, will give you a clue as to where in your script to look for a problem.

A syntax error can be detected by running your program using the Python interpreter.  The following command lets you check your script's syntax without actually running the script:

        $ python3 -m py_compile name.py

If a problem cause cannot be found in your script, then perhaps it is due to an unknown bug in Prevue.  You can reach me at:  r.e.hilburger@gmail.com


Copyright © 2025 by Richard E. Hilburger, All Rights Reserved        Prevue™