C. Titus Brown
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Server side Web programming with flask and jinja2 [toc] Titus Brown, ctbrown@ucdavis.edu. This text is under CC0, and the code is under BSD 3-clause. ## How the Web works ### Anatomy of a Web request & response ![Screenshot 2025-02-11 at 2.05.14 PM](https://hackmd.io/_uploads/ryk3YrKFJe.png) ### Browser sends a request: ![Screenshot 2025-02-11 at 2.02.55 PM](https://hackmd.io/_uploads/BJnUFHtYkl.png) ### Server sends a response: ![Screenshot 2025-02-11 at 2.03.00 PM](https://hackmd.io/_uploads/SkldKStYJe.png) ### Fun facts ![Screenshot 2025-02-11 at 2.03.12 PM](https://hackmd.io/_uploads/r1CqYrtYye.png) ## Building a Web server See https://github.com/ctb/2025-davis-pug-web-intro-flask, and the quickstart instructions in the README, for the example application we'll be going through. In brief, this application: * shows how to implement a basic Web application + index page in flask; * shows how to serve static content, too; * implements some minimal jinja2 templating; * uses a form GET; * uses a form POST; * adds CSS; * uses jinja2 templating to do variable interpolation; * uses jinja2 templating to do a for loop; * installs a custom jinja2 filter to display Markdown; ## The basics [Here](https://github.com/ctb/2025-davis-pug-web-intro-flask/blob/basic_only/main.py) is the most basic Flask app: it just creates an app object, and adds a single route that connects a URL to a function. Here we also take advantage of flask's default static file handling that (implicitly) routes '/static' requests to files in the `static/` directory. The key code is here: ```python! @app.route('/', methods=['GET']) def index(): return """hello, world! <p> <img src='/static/vase.jpg'/> <br> See: <a href='https://commons.wikimedia.org/wiki/File:33344-Mei%C3%9Fen-2008-Mei%C3%9Fner_Bleikristall_(Vase_und_Burg)-Br%C3%BCck_%26_Sohn_Kunstverlag.jpg'>source for vase image</a> """ ``` Key points: * any other URL than '/' errors; * the image request is to a _separate_ URL; ## Forms and templates [Here](https://github.com/ctb/2025-davis-pug-web-intro-flask/blob/forms_and_templates/main.py) is a slightly more advanced Web app, that does a number of things to get information from the client side. ### Custom routes with variables in them One way to send information from the browser is by constructing URLs carefully. Flask has a nice interface for converting URLs into Python function calls; see below, where `/user/somename` rsults in a call `user_fn('somename')` ```python! @app.route('/user/<username>') def user_fn(username): # here you would do some kind of validation for logins, etc. return f"this is the user page for user '{username}'" ``` ### Presenting a form; using templates This code: ```python! @app.route('/example_form', methods=['GET']) def example_form(): return render_template('example_form.html') ``` says, "use jinja2 to find and render the `example_form.html` template". If you look at [templates/example_form.html](https://github.com/ctb/2025-davis-pug-web-intro-flask/blob/forms_and_templates/templates/example_form.html) you'll see: ```html <h1>Example form</h1> <form method='GET' action='/example_form_process'> Input a number: <input type='text' name='the_number'></input> <input type='submit'> </form> <a href='./'>Return to index</a> ``` which is just straight HTML. This form says, "submit a value called 'the_number' to the URL `/example_form_process`". It's up to the Web app to say what to do with that value! ### Interpreting values submitted via a GET Form values submitted via GET are available in the `request.args` dictionary; the rest is just Python. ```python! @app.route('/example_form_process', methods=['GET']) def example_form_process(): value = request.args.get('the_number') if value is None: return redirect(url_for('example_form')) else: values=dict(the_number=value) return render_template('example_form_process.html', **values) ``` ### Interpolating Python values in a template Above, you'll note: ```python! values=dict(the_number=value) return render_template('example_form_process.html', **values) ``` which says, render the template, with the option to use the values in the passed in dictionary - here, obtained from the form! The template code itself is in [templates/example_form_process.html](https://github.com/ctb/2025-davis-pug-web-intro-flask/blob/forms_and_templates/templates/example_form_process.html) and looks like this: ```htmlembedded! Form value was: {{ the_number }} ``` Here, `{{` and `}}` tell Jinja2 that you want to interpolate a Python value - in this case, `the_number`. Note that the GET values show up in the URL, after the `?`. ### Presenting a POST form; checking values Let's look at another form - this time a POST form. The [template code](https://github.com/ctb/2025-davis-pug-web-intro-flask/blob/forms_and_templates/templates/example_form2.html) is just straight HTML, but a bit more complicated than before: ```html! <h1>Example form 2</h1> Let's generate a table of every number between 0 and some upper limit, multiplied by a custom number. <p> <form method='POST' action='/example_form_process2'> Upper limit: <input type='text' name='upper_limit' value='10'></input> Multiply by: <input type='text' name='multiply_by' value='4'></input> <input type='submit'> </form> <a href='./'>Return to index</a> ``` Note the `POST` instead of the `GET`. This isn't particularly important here - we could do this with a GET! - but it becomes important when you're accepting files, or other arbitrarily large inputs. The form processing code is here and is now a bit more complicated, because it does error checking: ```python! @app.route('/example_form_process2', methods=['POST']) def example_form_process2(): upper_limit = request.form.get('upper_limit') multiply_by = request.form.get('multiply_by') # error handling... try: upper_limit = int(upper_limit) multiply_by = int(upper_limit) except ValueError: upper_limit = None multiply_by = None if upper_limit is None or multiply_by is None: return redirect(url_for('example_form2')) else: values=dict(upper_limit=upper_limit, multiply_by=multiply_by) return render_template('example_form_process2.html', **values) ``` ### Using for loops in a template The [template file](https://github.com/ctb/2025-davis-pug-web-intro-flask/blob/forms_and_templates/templates/example_form_process2.html) is also more interesting here, because it does more of the work: ```html! Multiplying every number between 0 and {{ upper_limit }} by {{ multiply_by }}. <p> <table border=1> {% for i in range(0, upper_limit) %} <tr><td>{{ i }}</td><td>{{ multiply_by * i }}</td> {% endfor %} </table> ``` Here we're using a jinja2 for loop to produce a table. Let's look at the output HTML in the browser... ## Making things pretty; more advanced jinja2 stuff Here is our final Web app: [link](https://github.com/ctb/2025-davis-pug-web-intro-flask/blob/more_jinja2/main.py). Let's look at the new stuff! ### Installing and using jinja2 filters Jinja2 lets you install new functions, implemented in Python. [Here's one](https://github.com/ctb/2025-davis-pug-web-intro-flask/blob/more_jinja2/main.py#L7C1-L8C31) that makes a number into a percent: ```python! def percentify(num): return f"{num * 100:.1f}%" ``` and you can install it for Jinja2 use like so: ```python! # run this function once, to create the Flask applicaton. def create_app(): app = Flask(__name__) with app.app_context(): # do any configuration stuff here, including adding jinja2 filters. # add percentify filter app.jinja_env.filters['percentify'] = percentify return app ``` and you can use it in [a template](https://github.com/ctb/2025-davis-pug-web-intro-flask/blob/more_jinja2/templates/example_filter.html) like so: ```html! <h1>Example 'percentify' filter</h1> {{ val|percentify }} ``` ### A markdown filter My favorite filter so far is one that just takes Markdown and turns it into HTML. That way you can have [a file that you write in Markdown & renders on GitHub](https://github.com/ctb/2025-davis-pug-web-intro-flask/blob/more_jinja2/templates/example.md) but also is part of your Web app. Python code implementing the `markdownify` filter: ```python! def markdownify(md): return markdown.markdown(md) ``` The template code is then easy: ```htmlembedded! {% include "_header.html" %} {% filter markdownify %} # This is some markdown! Add your **markdown content** here!! {% endfilter %} {% include "_footer.html" %} ``` ### Making more use of jinja2 We can also make our HTML more pretty. I hate writing HTML more than once, but it's pretty easy to define files that you just include - take a look at our new [example form](https://github.com/ctb/2025-davis-pug-web-intro-flask/blob/more_jinja2/templates/example_form.html) code: ``` {% include "_header.html" %} <h1>Example form</h1> <form method='GET' action='/example_form_process'> Input a number: <input type='text' name='the_number'></input> <input type='submit'> </form> <a href='./'>Return to index</a> {% include "_footer.html" %} ``` here [`_header.html`](https://github.com/ctb/2025-davis-pug-web-intro-flask/blob/more_jinja2/templates/_header.html) is just stuff we want at the beginning: ```html! <html> <head> <title>Example Web app in flask</title> <link rel="stylesheet" href="/static/pico.min.css"/> </head> <body> <main class="container"> ``` and the same for the footer: ```html! </main> </body> </html> ``` Then, any changes we make to these files automatically get propogated across the site, as long as the templates use the right include. Note that here I'm using the [pico CSS library](https://picocss.com/docs) and serving up `pico.min.css` in [the `static/` directory](https://github.com/ctb/2025-davis-pug-web-intro-flask/tree/more_jinja2/static).

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully