Skip to main content
Chemistry LibreTexts

7.3: Scripting and Automation of the PG Renderer

  • Page ID
    557409
  • ​

    ​

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\ket}[1]{\left| #1 \right>}\)
    \(\newcommand{\bra}[1]{\left< #1 \right|}\)
    \(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
    \(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
    \(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
    \(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
    \(\newcommand{\expect}[1]{\langle #1 \rangle}\)
    \(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    Use this section when you need to render or lint problems programmatically rather than through the browser editor. Rather than writing scripts from scratch, start with the ready-made linting script below. It handles the API call, error checking, warning collection, and output formatting so you can focus on fixing problems instead of debugging your tooling.

    The examples below assume the renderer is already running on http://localhost:3000 (see Setting Up the PG Renderer for setup).

    Recommended linting script

    The lint_pg_via_renderer_api.py script is the fastest way to check a PG file from the command line. It reads a local .pg file, posts it to the renderer, and reports errors and warnings in one step. Copy the script below into your project or tools directory.

    Quick start

    # Lint a single file (random seed)
    python3 lint_pg_via_renderer_api.py -i my_problem.pg
    
    # Lint with a fixed seed for reproducibility
    python3 lint_pg_via_renderer_api.py -i my_problem.pg -s 42
    
    # View the rendered HTML output instead of lint messages
    python3 lint_pg_via_renderer_api.py -i my_problem.pg -r
    
    # Use a remote renderer instead of localhost
    python3 lint_pg_via_renderer_api.py -i my_problem.pg -b https://renderer.example.edu
    

    Command-line options

    Command-line options for lint_pg_via_renderer_api.py.
    Flag Default What it does
    -i / --input (required) Path to the local PG file to lint
    -s / --seed random Problem seed for reproducible results
    -b / --base-url http://localhost:3000 Renderer base URL
    -r / --render off Print rendered HTML instead of lint findings
    -o / --output-format classic Output format template id

    What the script checks

    The script collects messages from every place the renderer can report problems, so you do not need to remember which JSON field to inspect:

    • flags.error_flag – fatal rendering errors
    • errors and error – top-level error fields
    • warnings and warning – top-level warning fields
    • debug.pg_warn – PG-layer warnings (most common)
    • debug.internal and debug.debug – internal renderer messages
    • renderedHTML – scanned for embedded error blocks and "Translator errors" sections

    If nothing is found in any of these fields, the script prints "No lint messages detected." and exits cleanly. If error_flag is set, the script raises an error so it can be used in automated pipelines where a nonzero exit code signals failure.

    Batch linting

    To lint every .pg file in a directory, combine the script with a shell loop:

    # Lint all .pg files in a directory tree
    for f in $(find problems/ -name '*.pg'); do
        echo "=== $f ==="
        python3 lint_pg_via_renderer_api.py -i "$f" -s 42
        echo
    done
    

    Use a fixed seed (-s 42) so that every failure is reproducible. If you need to sweep multiple seeds, wrap the call in a second loop over seed values.

    Get the script

    Download lint_pg_via_renderer_api.py from the vosslab-skills GitHub repository and place it in your project directory. The script uses only the Python standard library (no pip install required).

    # Download the script directly
    curl -O https://raw.githubusercontent.com/vosslab/vosslab-skills/main/skills/webwork-writer/references/scripts/lint_pg_via_renderer_api.py
    

    API reference

    This section documents the renderer API for readers who want to understand what the script does or build custom integrations.

    Endpoints

    API endpoints: endpoint, method, and use for.
    Endpoint Method Use for
    /render-api POST Primary endpoint for rendering problems (used by the linting script)
    / POST Alternative rendering endpoint (same behavior)
    /health GET Check if the renderer is running and accepting requests

    Input parameters

    The renderer accepts three input types for specifying problem source. If you provide more than one, the renderer uses the highest-precedence source and ignores the rest.

    Precedence order: problemSourceURL (highest) > problemSource (middle) > sourceFilePath (lowest)

    Input parameters: parameter, what it is, and use when.
    Parameter What it is Use when
    problemSource Raw PG source as a string Source is read from a local file (this is what the linting script uses)
    problemSourceURL URL to a JSON endpoint that returns problem source Problem source lives on a remote server
    sourceFilePath Path relative to the renderer's private/ or Library/ directory File exists inside the renderer container

    Response fields

    The renderer returns a JSON object. Key fields:

    Response fields: field, type, and description.
    Field Type Description
    renderedHTML string The rendered problem HTML (student view)
    flags.error_flag int 0 = success, 1 = rendering error
    debug.pg_warn array Warning messages from the renderer
    answers object (Instructor mode) Answer structure with correct values

    Checking renderer health

    Before linting or rendering, confirm the renderer is running. The /health endpoint returns a simple status check:

    curl http://localhost:3000/health
    

    A healthy renderer responds with a JSON object. If the request times out or returns a connection error, the container is not running. See Setting Up the PG Renderer for startup steps.

    If you are scripting a pipeline, check health before submitting files:

    import urllib.request, json
    
    def check_health(base_url="http://localhost:3000"):
        try:
            req = urllib.request.urlopen(f"{base_url}/health", timeout=5)
            return req.status == 200
        except Exception:
            return False
    
    if not check_health():
        print("Renderer is not running -- start the container first.")
    

    Curl render example

    If you prefer a quick one-off render check without Python:

    curl -X POST "http://localhost:3000/render-api" \
      -H "Content-Type: application/json" \
      -d '{
        "problemSource": "DOCUMENT(); loadMacros(\"PGstandard.pl\",\"PGML.pl\",\"PGcourse.pl\"); BEGIN_PGML Hello END_PGML ENDDOCUMENT();",
        "problemSeed": 42,
        "outputFormat": "classic"
      }'
    

    You can also use the form-encoded style with -F flags if the problem source is in a file mounted inside the container:

    curl -X POST "http://localhost:3000/render-api" \
      -F "sourceFilePath=private/myproblem.pg" \
      -F "problemSeed=1234" \
      -F "_format=json"
    

    Checking for success manually

    A clean render has error_flag == 0 and an empty pg_warn array. Always check both, because a problem can render without a fatal error but still produce warnings. The linting script handles this automatically, but if you are writing your own integration:

    error_flag = data.get('flags', {}).get('error_flag', 0)
    warnings = data.get('debug', {}).get('pg_warn', [])
    
    if error_flag:
        print('Rendering failed completely')
    elif warnings:
        print('Rendered with warnings:')
        for w in warnings:
            print(f'  - {w}')
    else:
        print('Clean render, no issues')
    

    Apply it today

    • Copy lint_pg_via_renderer_api.py into your project and lint a file with python3 lint_pg_via_renderer_api.py -i my_problem.pg.
    • Use -s 42 when reporting bugs so the failure is reproducible.
    • Use -r to view the rendered HTML when you need to check formatting.
    • Wrap the script in a shell loop to batch-lint an entire directory before publishing.
    • The script uses only the Python standard library, so no pip install is needed.

    This page titled 7.3: Scripting and Automation of the PG Renderer was last modified on Fri, 27 Feb 2026 15:47:10 GMT and is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Neil R. Voss.