Skip to content
ReferenceCoreLook up every set_ and add_ configuration helper.

Site configuration functions

These functions configure the whole site rather than one page. They all follow the same placement rule: call them at the top level of your program, after the imports and before start_server(...). None of them belong inside a route.

from drafter import *

set_website_title("Fortune Teller")
set_website_style("water")


@route
def index() -> Page:
    return Page([
        "The site title and theme were set before the server started.\n",
        Button("Consult the orb", "index")
    ])


start_server()

Under each heading below, the first line gives the function's parameters and any default values. The code block after it shows a typical call. Every function also accepts a final server keyword, which chooses the app to configure in a program that runs more than one. Ordinary programs leave it out.

Some of these settings can also be given as keywords to start_server. Both forms write the same settings, and the one written last wins. A start_server keyword therefore wins over a configuration function called above it: with set_website_title("A") above start_server(site_title="B"), the title is "B".

Appearance

set_website_title

set_website_title(title)

set_website_title("My Amazing App")

Sets the title shown in the browser tab.

set_website_favicon

set_website_favicon(favicon)

set_website_favicon("icon.png")

Sets the small browser-tab icon. Give the name of an image file (svg, png, ico, ...) next to your Python file, or a full URL to an image. When deploying, remember the file must travel with your site; see Image works locally but not deployed.

set_website_style

set_website_style(style)

set_website_style("terminal")

Applies a theme by name. set_website_style("none") removes all styling for a blank slate. A misspelled name produces an error listing every valid theme. The how-to is Use a theme.

set_website_framed

set_website_framed(framed)

set_website_framed(False)

Controls whether your app sits inside Drafter's window frame. The frame is helpful during development; deployed sites usually turn it off so the app fills the page.

set_page_transition

set_page_transition(transition="fade", duration=None)

set_page_transition("fade")
set_page_transition("black", 0.3)

Plays a visual transition whenever the visitor moves between pages. "fade" fades the new page in from transparent; a CSS color such as "black" or "#004488" fades in from a solid veil of that color; "none" turns transitions off again. The second argument is the duration in seconds. When it is left out, the duration keeps its current value, which starts at 0.5 seconds.

set_button_spinners

set_button_spinners(enabled=True)

set_button_spinners(True)

When spinners are turned on, a pressed button shows a small spinner and is temporarily disabled while its request is being processed. This is helpful when a route does slow work and a visitor might otherwise click twice.

set_state_display

set_state_display(enabled=True)

set_state_display(False)

The debug panel normally shows your whole state after every change. When the state is very large, such as a long list, a big grid, or many pictures, that can slow each page down and push everything else in the panel out of view. Turning the display off replaces it with a one-line note.

Only the display changes. Your state still works the same way, and so do the back and forward buttons and the panel's Save and Load. Call set_state_display(True) to bring the display back.

set_browser_history

set_browser_history(enabled=True)

set_browser_history(False)

Normally the browser's back and forward buttons move through your app: each page a visitor opens becomes an entry in the browser's history, and going back restores that page along with the state it had at the time.

Turn this off when your app is placed inside another web page, such as a course page that shows several small apps. There the back button belongs to the outer page, and a visitor who presses it expects to leave that page, not to rewind your app. With history off, Drafter never adds entries to the browser's history and never changes the address bar.

Call it before start_server(...); the setting is read once, when the site starts. The debug panel's own history and replay tools keep working either way. The live demos in this documentation turn history off automatically. The same setting is available as the --no-browser-history command line flag.

Adding raw content

add_website_header

add_website_header(header)

add_website_header("<meta name='author' content='Ada'>")

Adds raw content to the site's HTML head: meta tags, script tags, or anything else that belongs there. The text is not wrapped or escaped.

add_website_css

add_website_css(selector, css=None)

add_website_css("h1 { color: darkslateblue; }")
add_website_css("h1", "color: darkslateblue;")

Adds CSS to every page. With one argument, the text is raw CSS. With two, the first is a selector and the second is the rule body to wrap in braces. The how-to, including the selectors you need, is Custom CSS. Passing a file name such as "style.css" is a mistake this function catches and reports; use add_website_css_file for files.

add_website_css_file

add_website_css_file(path)

add_website_css_file("style.css")
add_website_css_file("https://fonts.googleapis.com/css2?family=Lora")

Links a stylesheet into every page. The argument is either the name of a .css file next to your Python program (a subfolder path such as "static/style.css" also works) or a full URL. Files next to your program are checked immediately: a missing file raises an error that names the folder Drafter searched and suggests similarly named files, so "style.css" when the file is "styles.css" is caught at once rather than silently ignored. A name that differs from the file only in its capital letters is an error too, as described under add_website_file. The file is registered as a website file (see add_website_file), so it is copied into the built site when you deploy. Your stylesheet is linked after the theme's, so its rules win ties.

add_website_js

add_website_js(js)

add_website_js("console.log('Site loaded');")

Runs JavaScript code once, when the site first loads. Like add_website_css, it takes code, not a file name.

While you are developing, each save runs your program again in the same browser tab, and this code runs again with it. Cleaning up before a reload shows how the code can remove what an earlier run set up.

add_website_js_file

add_website_js_file(path)

add_website_js_file("app.js")
add_website_js_file("https://cdn.example.com/library.js")

Loads a JavaScript file on every page, from a .js file next to your program or from a full URL. Local files get the same checks and suggestions as add_website_css_file, and are registered as website files for deployment.

add_website_file

add_website_file(*filenames)

add_website_file("words.txt", "images/logo.png")

Declares files next to your program that the site needs at runtime: data files you open(), images, fonts, and so on. A name can also be a folder, as in add_website_file("images"), which declares everything inside it. The files are copied into the built site when you deploy. This is the way to prevent the works locally, 404s deployed problem. Only files inside your program's folder can be added: absolute paths, .. paths, and URLs are rejected with an explanation.

Each name is checked as soon as the line runs, and a name that fails the check stops the program. The terminal shows a titled message in place of a traceback. It gives the line of your program, an explanation, and a list of things to try.

  • No file or folder has that name. The message is titled "Website File Not Found". When a similarly named file exists, the first thing to try is a suggestion such as "Did you mean 'questions.txt'?".
  • The capital letters differ. Windows and macOS open Logo.PNG when a program asks for logo.png. The server that hosts a deployed site does not, so Drafter stops the program on your own computer, before the site is deployed:

    ==============================================================================
    Website File Name Has Different Capital Letters
    ==============================================================================
    Where: main.py, line 3
    
    Your program asks for 'logo.png', but the file is named 'Logo.PNG'. Your
    computer treats those as the same name. The server that hosts a deployed site
    does not, so the site would not find the file there.
    
    What to try:
      1. Change the name in your code to match the file:
         add_website_file('Logo.PNG').
      2. Or rename the file so its capital letters match your code.
    
    Details: add_website_file: 'logo.png' is spelled 'Logo.PNG' on disk (the
             capital letters differ)
    

add_website_css_file and add_website_js_file make the same two checks. They accept a file only, not a folder.

Two command line flags also copy files into the built site. --additional-files is the flag form of this function and takes one file or folder name at a time. --additional-paths is a separate build flag that also accepts patterns such as data/*.csv. Calling add_website_file in your program has the advantage that the list of files travels with the code.

Site information

set_site_information

set_site_information(author, description, sources, planning, links)

set_site_information(
    "Ada Lovelace",
    "An app for tracking my corgi's snacks.",
    "Icons from openmoji.org",
    "See planning.pdf in the repository.",
    ["https://github.com/ada/snack-tracker"]
)

Records who made the site and how, shown on the site's built-in --about page. All five arguments are required, in order: author, description, sources, planning, and links (a list). Use empty strings, or an empty list for links, for anything that does not apply. Deployment expects this to be filled in; see Prepare for release.

get_site_information

get_site_information()

get_site_information()

Returns the information previously set, mostly useful for checking it in tests.

Debugging and release

hide_debug_information

hide_debug_information()

hide_debug_information()

Turns off the debug panel. This is the standard step before deploying, so visitors see your app rather than your debugger.

show_debug_information

show_debug_information()

show_debug_information()

Turns the debug panel back on. The panel is on by default during development.

set_error_page

set_error_page(title=None, message=None, show_details=None)

set_error_page("Oh no!", "Something broke. Try the home page.", False)

Customizes the built-in error page. The three arguments set the heading, the friendly message shown to visitors, and whether technical details (the error message and traceback) are shown. Each argument is optional, and one that is left out keeps its current setting. Deployed sites often pass False so visitors never see a traceback.

deploy_site

deploy_site(image_folder="images")

deploy_site()

Prepares the site for deployment. Currently it turns off debug information, the same as hide_debug_information(); the image_folder argument is accepted for compatibility but not yet used.