python.exe get-pip.pyLinux -
python get-pip.pyActually using pip to install flask:
pip install flaskGreat, now Flask is installed as a python library and can be used in any python script.
#!/usr/bin/python2
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
#!/usr/bin/python2This can be ignored for windows users. If you're using linux this tells the compiler when you run the file as an executable to compile it with python2
from flask import FlaskHere we are adding the flask library so you are able to use it.
@app.route("/")This is the interesting part of Flask. Here you define what happens when a user goes to any url on your website. Example if you wanted a user to see "Hi" when they go to http://site.com/garbage/he
@app.route("/garbage/helloand use the code below to serve what they see")
Here the Flask interpreter will run this function/method which returns "Hello World!" and it will serve this when you go to "http://localhost.com:5000/
def hello():
return "Hello World!"
This should be pretty self-explanatory if you're familiar with Python. Basically this initializes Flask and the program.
if __name__ == '__main__':
app.run()
chmod +x main.py
./main.py
or alternatively
python main.py
<!DOCTYPE html>
<html>
<head>
<title>EE Tutorial</title>
</head>
<body>
<header>
<div class="container">
<h1 class="logo">Experts Exchange</h1>
</div>
</header>
<div class="container">
{% block content %}
{% endblock %}
</div>
</body>
</html>
{% extends "style.html" %}
{% block content %}
<div class="jumbo">
<h2>"The ride never ends"<h2>
<h3>- Mr. Bones<h3>
</div>
{% endblock %}
#!/usr/bin/python2
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
if __name__ == "__main__":
app.run(debug = True)
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)