liveBook · Manning

State management

As explained in Build Python Web Apps with Streamlit

State management is the practice of retaining and updating application data that determines behavior across interactions or executions.

State management keeps an application's changing data available as users move through interactions. In a quiz app, the state records the current question, answer, evaluation, and the game's next step. section 9.1.1 section 9.4

A practical stateful design for the quiz app:

  • Store the Game instance in Streamlit's session state so the same instance survives reruns. section 9.4
  • Track status explicitly, beginning with GET_QUESTION and advancing through question display, answer evaluation, and the result. section 9.4
  • Keep curr_question, curr_answer, and curr_eval inside Game instead of passing these values between unrelated functions. section 9.4
  • Branch the frontend on the current status, then rerun after obtaining a question, submitting an answer, or evaluating it. section 9.4

Fact Frenzy quiz flow

The app obtains an LLM-generated question, displays it for an answer, evaluates the submitted response, and reports whether it is correct. Persistent storage, user accounts, and category selection remain outside the design. section 9.1.1 section 9.4

Listing 9.7 Moving through game states in a modified game.py
from llm import Llm
from prompts import QUESTION_PROMPT, ANSWER_PROMPT
from answer_evaluation import AnswerEvaluation

class Game:
  def __init__(self, llm_api_key):
    self.llm = Llm(llm_api_key)
    self.status = 'GET_QUESTION'

    self.curr_question = None
    self.curr_answer = None
    self.curr_eval = None

  def ask_llm_for_question(self):
    usr_msg, sys_msg = QUESTION_PROMPT['user'], QUESTION_PROMPT['system']
    return self.llm.ask(usr_msg, sys_msg)

  def ask_llm_to_evaluate_answer(self):
    sys_msg = ANSWER_PROMPT['system']
    user_msg = (
      ANSWER_PROMPT['user']
      .replace('{question}', self.curr_question)
      .replace('{answer}', self.curr_answer)
    )
    reply = self.llm.ask(user_msg, sys_msg, AnswerEvaluation)
    return reply

  def obtain_question(self):
    self.curr_question = self.ask_llm_for_question()
    self.status = 'ASK_QUESTION'
    return self.curr_question

  def accept_answer(self, answer):
    self.curr_answer = answer
    self.status = 'EVALUATE_ANSWER'

  def evaluate_answer(self):
    self.curr_eval = self.ask_llm_to_evaluate_answer()
    self.status = 'STATE_RESULT'

Inspect the Game implementation to see status initialization, current-value attributes, and methods that advance the quiz state. section 9.4

How Build Python Web Apps with Streamlit covers it

How deep it goes
Defines a stateful Game object, session storage, current values, and status-driven branches; defers persistent storage.
Examples
Demonstrates Fact Frenzy through question retrieval, answer submission, evaluation, and result display.
What you can do after
Apply it via Streamlit to preserve a Game instance and move a quiz through sequential states.
How it connects
Connects state management with Streamlit session state, conditional branching, reruns, and LLM-based question evaluation.

Also explained in

Compare how 9 books explain state management →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.