Flask App in 5 Minutes

Search for a command to run...

Very Nice and simple set up. I bumped into it while looking for HTMX resources.So I will look very closely to your other post about HTMX.
In your blog post I noticed two small typos in your routes.py. @app.route('/welcome') def hello(): name = 'AJ' return render_template("index.html", value=name)
The default route is simply @app.route('/') and the rendering should be name=name rather than value=name.
Again Thanks for your post !!
A quick cheatsheet for TMUX commands that I can never remember. tmux yum -y install tmux Out of tmux new session tmux new new session with name tmux new -s sessionname show sessions tmux ls attach to session tmux attach-session -t 0 Go to last sessi...
Here's a fun error: "not syncing vfs unable to mount root fs on unknown block" I'm running CentOS7 on Linode and happened to run into this error on one of my systems. Quite unusual, so I did the quick and dirty method and restored a backup of my VM....
I discovered a fun git command that I wasn't aware of before. git switch -c <new-branch> Using git switch, you can take existing, uncommitted files and move them to a new branch. Say you're working in the master branch and want to move those changes...
Well, maybe not so hidden, but definitely not advertised. Vudu has an API that, for the life of me, I could not find advertised anywhere, even with an advanced Google search. So, I'll take it upon myself to scrape their webpage and dissect their ho...
I didn't realize until recently that you can run DynamoDB on a local machine on either Windows, Mac, or Linux. This obviously can be hugely benefitial for learning and dabbling in DynamoDB, without racking up a large AWS bill. Let's see if we can get...
I'm a fan of Flask. Flask is a micro web framework written in Python. Here's a quick start guide to creating a new Flask application.
Create a new directory. Create a file called web.py with this contents:
from routes import app
if __name__ == "__main__":
app.run(debug=True, port=5000, host='0.0.0.0')
Feel free to change the above settings as you please.
Save that. Create a routes.py file with this contents:
from flask import Flask, render_template
app = Flask(__name__)
app.config.from_object(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
Now create your flask venv. I'll use pipenv for this
pipenv --three
pipenv install flask
pipenv run python web.py
Open a web browser to http://localhost:5000 and see your "Hello, World!"
That's literally all there is to it, but this is kind of boring. Let's set up another route.
First, create a folder called 'templates' and within, create a file called layout.html

The layout.html file should look like this. Throw a stylesheet in for fun. I'm using Bulma cause I like the name (references!):
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Quick Flask!</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css">
</head>
<body>
<H1>Flask app!</H1>
<br />
{% block content %}
{% endblock %}
</body>
</html>
Create another template file called "welcome.html" in the same folder with this contents:
{% extends "layout.html" %}
{% block content %}
<p><em>This is the Welcome page!</em></p>
{% endblock %}
Return to the routes.py file and add another route right under our default route.
@app.route('/welcome')
def welcome():
return render_template("welcome.html")
Restart your flask app and browse to http://localhost:5000/welcome You should see content from the layout.html page as well as the contents we added to the welcome.html page.
Pretty simple, but mostly useless. Let's add something a little more dynamic. Let's change the default route to be more than Hello, World!
@app.route('/welcome')
def hello():
name = 'AJ'
return render_template("index.html", value=name)
Let's create the template that we reference here. Create a new file called index.html in the templates folder with this contents:
{% extends "layout.html" %}
{% block content %}
<p><em>Hello, {{ name }}. Welcome to my app!</em></p>
{% endblock %}
Save it and restart your flask app. Better, but let's let the user decide what their name should be. This will require us to import "request", so in the routes.py file, add that import.
from flask import Flask, render_template, request
Within index.html, create a simple HTML form to prompt for name. The full page should look like this:
{% extends "layout.html" %}
{% block content %}
<p><em>What is your name?</em>
<form action="/welcome" method="POST"><input name="myName"><input type="submit" value="Submit"></form>
</p>
{% endblock %}
Next, edit the welcome route in the routes.py file to request the myName field from the previous page.
@app.route('/welcome', methods=['POST'])
def welcome():
return render_template("welcome.html", myName=request.form['myName'])
Finally, update the welcome.html page to get the Name from the post.
{% extends "layout.html" %}
{% block content %}
<p><em>This is the Welcome page! Hello {{ myName }}</em></p>
{% endblock %}
I hope you enjoyed this little flask quickstart guide.
Refresh and you'll get a form that you can enter your name. Submit it and you'll be sent to the welcome page with your name on it.


That's it for today!
AJ