Skip to content

Instantly share code, notes, and snippets.

@x
Created July 15, 2024 23:02
Show Gist options
  • Select an option

  • Save x/e25c7cc8ad36cc973711cef9631b8bf7 to your computer and use it in GitHub Desktop.

Select an option

Save x/e25c7cc8ad36cc973711cef9631b8bf7 to your computer and use it in GitHub Desktop.

Revisions

  1. x created this gist Jul 15, 2024.
    137 changes: 137 additions & 0 deletions hello.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,137 @@
    """Here's Mr. P's example of a simple Flask app.
    How to use this:
    1. Install Python
    2. Install Flask
    3. Download this file and make sure it's named "hello.py"
    4. Run this file using:
    flask --app hello run --debug
    5. Open your browser and go to:
    http://127.0.0.1:5000
    You should see "Hello, World!"
    For more about Flask, see the official documentation:
    - https://flask.palletsprojects.com/en/2.0.x/
    If you'd like to deploy your Flask app to the web, you can use a service like
    Render or Fly.io:
    - https://render.com/
    - https://fly.io/
    """

    from flask import Flask, request

    # App is a special "singleton object" that represents the Flask application, we
    # use it to assign "routes" to functions.
    app = Flask(__name__)


    # This is a "route" that tells Flask to run the function "hello_world" when the
    # user goes to the URL "http://127.0.0.1:5000/".
    @app.route("/")
    def hello_world():
    return "<p>Hello, World!</p>"


    # This is a "route" that tells Flask to run the function "hello_name" when the
    # user goes to the URL "http://127.0.0.1:5000/hello/<name>", where <name> is a
    # placeholder for any string.
    # Example:
    # If I go to "http://127.0.0.1:5000/hello/John"
    # I will see the message "<p>Hello, John!</p>".
    @app.route("/hello/<name>")
    def hello_name(name):
    return f"<p>Hello, {name}!</p>"


    # Here's an example of rendering a form in HTML. When the user submits the form,
    # the data will be sent to the "/submit" route.
    # Get to this page by going to "http://127.0.0.1:5000/form".
    @app.route("/form")
    def form():
    return """
    <html>
    <head>
    <title>Form</title>
    </head>
    <body>
    <h1>Form</h1>
    <form action="/submit" method="post">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name">
    <input type="submit" value="Submit">
    </form>
    </body>
    """


    # This is a "route" that tells Flask to run the function "submit" when the user
    # submits the form in the "/form" route.
    @app.route("/submit", methods=["POST"])
    def submit():
    name = request.form["name"]
    return f"<p>Submitted name: {name}</p>"


    # ------------------------------------------------------------------------------
    # ----- A Simple Adventure Game ------------------------------------------------
    # ------------------------------------------------------------------------------
    # Below is a simple text-based choose-your own adventure game that uses Flask to
    # create a web interface for it. In this game, the player starts on a page with
    # two links, one to the left and one to the right. Clicking the link will call
    # the corresponding route and display a message.
    # ------------------------------------------------------------------------------


    # Get to this page by going to "http://127.0.0.1:5000/start".
    @app.route("/start")
    def start():
    # Note: A better way to render HTML is with templates, but this is a simple
    # example to get you started.
    html = """
    <html>
    <head>
    <title>Choose Your Own Adventure</title>
    </head>
    <body>
    <h1>Choose Your Own Adventure</h1>
    <p>Welcome to the adventure! You are standing in a dark room. You see a door to your left and a door to your right.</p>
    <a href="/left">Go left</a>
    <a href="/right">Go right</a>
    </body>
    """
    return html


    # Get to this page by going to "http://127.0.0.1:5000/left".
    @app.route("/left")
    def left():
    html = """
    <html>
    <head>
    <title>Choose Your Own Adventure</title>
    </head>
    <body>
    <h1>Choose Your Own Adventure</h1>
    <p>You went left and found a treasure chest! You win!</p>
    <a href="/start">Start over</a>
    </body>
    """
    return html


    # Get to this page by going to "http://127.0.0.1:5000/right".
    @app.route("/right")
    def right():
    html = """
    <html>
    <head>
    <title>Choose Your Own Adventure</title>
    </head>
    <body>
    <h1>Choose Your Own Adventure</h1>
    <p>You went right and fell into a pit of spikes! You lose!</p>
    <a href="/start">Start over</a>
    </body>
    """
    return html