Write static pages¶
When you site is done, you can continue to serve it within your Flask app, or you can write static html pages ready to be published on the web as a static site.
Almoststatic has the method write_static
which write static pages on the
disk. It only write the pages in the location that you choose.
The is up to you to publish your pages where you need and also to copy your static contents.
The best way to write static site is to write a little script, the core of script should be:
import sys
import almoststatic
a = almoststatic.Almoststatic()
a.load_config()
STATIC_URL = '/'
MEDIA_PREFIX = 'media'
DESTINATION = './_static_site'
if len(sys.argv) > 1:
STATIC_URL = sys.argv[1]
print(STATIC_URL)
a.write_static(destination=DESTINATION,
media_prefix=MEDIA_PREFIX,
static_url=STATIC_URL)
a.write_json_meta(destination=DESTINATION)
The destination
parameter is the location where files will be written in
your disk.
The static_url
parameter lets you to set location of files starting from
your url, i.e. if your site goes on https://mystatic.example.com/my_pages/
static_url
will be /my_pages/
. If your pages will be read from file
system you should leave static_url
as an empty string so urls are treated
as relative. If files goes on root of your domain static_url
should be /
because this optimize the browser cache.
In this case we added the capability to add static url as argument to the script.
The media_prefix
parameter let you to change the location of media files
from ‘/content/media’ which is the default in the development environment to
the location where they will be in static site.
Consider that the write_static
when is working changes the global variable
is_static
from False to True, so if in development time you have some pages
that you don’t like to have in the static site, you can exclude them inspecting
the value of that variable.
By default, write_static
writes all pages of site, but if you need, you
can choose the pages to write setting the pages
parameter or the
out_pages
parameter.
You have also the write_json_meta
method that let you write on a json file,
all metadata for pages. This can be used to write some JavaScript on client
side to access to information about pages, but Almoststatic always write a file
called site_meta.json
on the root of media
folder, so this method often
is not required.
If you wish, you can add to your script all you need to automate the publishing
of site, like client ftp commands or automatic copying of media files and so on,
see flaskapp.py
of the sample folder to see automatic ftp publishing in
action.