[Python][Flask] Why a Flask Route Cannot Wait for User Input: Understanding Stateless HTTP

Why a Flask Route Cannot Wait for User Input: Understanding Stateless HTTP

How did I encounter this?

For GTPS, I want to implement a feature where the user chooses the target game to add into his/her list. I was thinking about a seamless experience where the user searches, chooses the game to choose while the server waits for the chosen game. I thought it's possible considering what I have done with the command-line implementation with the python code. Then what I found is that a Flask route cannot pause, wait for a user's input and resume. The core reason lies in the nature of HTTP itself.

HTTP Is Stateless

HTTP (Hypertext Transfer Protocol) is the foundation of data communication on the web. It is a stateless protocol, meaning:

Each request from a client (browser) to a server is independent.
The server does not retain any memory or execution context between requests.

Once the server sends a response, the connection is closed. There is no ongoing session, process, or waiting unless manually re-established by the client through a new request.


Comparison between the terminal input and web input

Terminal Input

In a terminal-based Python script, it’s possible to pause and wait for user input:

# Command-line interaction
candidates = ["Game A", "Game B", "Game C"]
print("Select a game:")
for i, c in enumerate(candidates):
    print(f"{i + 1}. {c}")
choice = input("Enter number: ")

In this case, the program waits until the user types something. It all happens within one continuous runtime environment.

Web Input (Flask)

@app.route('/search', methods=['POST'])
def search():
    keyword = request.form['keyword']
    candidates = search_candidates(keyword)
    # Can't wait here for user to pick one
    return render_template('select_game.html', candidates=candidates)

In this Flask route:

  • The keyword is received.

  • Candidate games are listed and rendered in a template.

  • But the function must return a response—it cannot “pause” and wait for the user to click one.

Once the response is sent, the route’s execution ends. There’s no way to re-enter this function later.


Correct Web-Based Approach: Split Into Multiple Requests

Instead of trying to pause, the logic is split across multiple routes:

1. Search Step

@app.route('/search', methods=['POST'])
def search():
    keyword = request.form['keyword']
    candidates = search_candidates(keyword)
    return render_template('select_game.html', candidates=candidates)

2. Selection Step

@app.route('/select', methods=['POST'])
def select():
    selected_game_id = request.form['selected_game']
    game_data = get_game_by_id(selected_game_id)
    return render_template('game_details.html', game=game_data)

Each step is a separate request. The user clicks a game candidate, triggering a new POST to the /select route.


Comments