Section 5.11 Testing HTML Output Locally
Certain complicated parts of HTML output will not always function when you look at PreTeXt output by just opening files in your web browser. These include knowls, Sage cells, and YouTube videos. This is a consequence of security policies and so will vary from browser to browser. A solution is to run a web server on your own machine, which is much easier than it sounds.
In fact, this is exactly what the PreTeXt-CLI does when you type the following command.
$ pretext view web
You can replace “web” with any target name, although it is really HTML builds that benefit from this feature. The CLI uses Python to spin up a local web server so you can see a copy of your output by going to the URL the CLI gives as output (usually
http://localhost:8000
, although if you are working in CoCalc, this will be different).There are two useful options that can be used with
pretext view
to speed up authoring. First, the -b
flag will build your source before starting the server (i.e., runs pretext build
in the background). Similarly, the -g
flag will generate assets prior to viewing. For small projects, adding the -w
flag will “watch” your source for changes and rebuild the HTML output whenever the source changes (this also builds the source before starting the server).It is also possible to specify whether your server is public or private (on your local network) with the
-a
option, and to specify a port other than 8000 with the -p
option. (Run pretext view --help
for more.)Having this quick local web server might be useful for other purposes. You can use the
-d
option to specify a path to any directory to serve any files, not just those created in PreTeXt.Another option, in the case where you know where your output lives (such as when you use the
pretext/pretext
script) is to use Python itself to start a simple web server. Ideally, first set your working directory to the location of your HTML output. Then in a console, at a command-line:$ python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
Which tells you to open your browser using the address for more options.
http://0.0.0.0:8000/
. Ctrl-C
in the terminal will kill the server when you are done. See the Python documentation1
docs.python.org/3/library/http.server.html