Skip to content
ReferenceFirst stepsKnow every student-relevant start_server option.

start_server

start_server(...) hands your routes over to Drafter and starts the site. It is the last line of every Drafter program. Code written after it does not run when you would expect.

Syntax

start_server()
start_server(initial_state)
start_server(initial_state, theme="terminal", site_title="My App")

The first argument is your starting state, usually an instance of your State dataclass. Leave it out for an app with no state.

from drafter import *
from dataclasses import dataclass


@dataclass
class State:
    score: int


@route
def index(state: State) -> Page:
    return Page(state, [
        "Score: " + str(state.score) + "\n",
        Button("Add a point", "gain")
    ])


@route
def gain(state: State) -> Page:
    state.score = state.score + 1
    return index(state)


start_server(State(0))

Student-relevant options

All options other than the state are keyword arguments.

Option Type Default What it does
initial_state any None The starting state, passed to index on the first visit and restored by the built-in --reset route.
theme str "default" Apply a theme by name, same effect as set_website_style(...).
site_title str "Drafter Application" The title shown in the browser tab.
favicon str Drafter's own icon The small browser-tab icon: a URL or the path to an image file next to your program.
framed bool True Whether the app sits inside Drafter's window frame. set_website_framed(False) is the usual way to turn this off for deployment.
in_debug_mode bool True Whether the debug panel appears. False shows the site the way visitors will see it, which is the same as the --production flag. hide_debug_information() is the usual way to turn the panel off.
console_mode str "auto" Where print() output appears in the browser. "auto" shows a console in the page footer while the debug panel is on. "hover" shows a floating console box, which also appears when the debug panel is off. "toast" shows each line briefly in a corner, and "devtools" sends output only to the browser's developer console.
port int 8000 Which port the local development server listens on. The default port moves to the next free one when it is busy. A port you choose is never moved.
host str "localhost" Which network interface the local development server binds.
open_browser bool True Whether running locally opens a browser tab automatically.
browser str None Which browser opens the tab: "chrome", "firefox", "safari", "edge", or "default" for your computer's usual browser. When this is not set, Drafter uses Chrome if installed, then Firefox, then the default.
engine str "pyodide" Which in-browser Python runs your code. "pyodide" is the one these docs cover.
use_reloader bool True Whether saving a file restarts the site automatically. Live reload covers this and the finer-grained watch_... options.
skip bool False Do nothing at all: no server, no compilation. Meant for test runners; see the note below.

Several options exist in two forms: a start_server keyword and a configuration function (set_website_style, set_website_title, set_website_framed, hide_debug_information). The configuration functions read more clearly and are what the rest of these docs use.

Both forms write the same settings, and the one written last wins. start_server(...) runs after every configuration function above it, so its keywords win. With set_website_title("A") above start_server(site_title="B"), the browser tab shows "B". Setting each option in only one of the two forms keeps a program easier to read.

A misspelled option is ignored, with a warning

start_server accepts any keyword. A keyword that it does not recognize, such as sight_title in place of site_title, is ignored, and the real option keeps its default. The site still starts, and the terminal prints a warning that names the keyword:

Warning: Unknown start_server option 'sight_title'. Did you mean 'site_title'? The option was ignored.

In the browser, the debug panel lists the same problem as a warning. The "Did you mean" part is left out when no real option has a similar name. If an option seems to have no effect, look for this warning and check the spelling against the table above.

Where settings come from

A setting can be given in three places. The examples all set the theme.

  1. A start_server keyword. The setting is part of your program, so it is still in effect when the site is deployed.

    start_server(State(0), theme="terminal")
    
  2. A command line flag. The setting applies to one run. Command line lists the flags.

    drafter my_app.py --theme terminal
    
  3. An environment variable. The variable's name is DRAFTER_ followed by the setting's name in capital letters.

    DRAFTER_THEME=terminal python my_app.py
    

    That line is written for macOS, Linux, and Git Bash. In Windows PowerShell, set the variable on its own line first with $env:DRAFTER_THEME = "terminal", and then run the program.

When the same setting is given in more than one place, a start_server keyword wins over a flag, and a flag wins over an environment variable.

Notes

  • Code after the call runs at the wrong time. start_server takes over the program, so all configuration must come before it. On your computer, code placed after the call waits until the server stops. In the browser, it runs right after the first page appears. When the file has statements after the call, the terminal prints a warning that names the line, and the debug panel lists the same warning. Comments and blank lines after the call do not count. See Code after start_server never runs.
  • Tests run first. Assertions written above start_server execute before the site starts. A failing assertion prints a FAILURE message and the site still starts. See Testing functions.
  • Skipping the server for tests. Setting the DRAFTER_SKIP environment variable (or passing skip=True, or running with the --skip flag) makes start_server return immediately without starting the server or compiling anything, so a unit test runner can import your file without launching the site.
  • Watching a large folder. If your program sits in a broad folder like Downloads or your home directory, Drafter prints a notice and watches only your program and the files your site uses, instead of the entire folder. Live reload has the details.
  • Old v1 options are ignored. Options that only Drafter v1 had, such as title, style, and debug, do nothing and produce no message, so a program written for v1 still starts. The cdn_skulpt options print a warning and do nothing.
  • Command-line flags set many of the same options for one run of drafter yourfile.py. A keyword in start_server wins when both are given. See Command line and Where settings come from.