I’ve been using Flask in Cloud9 to create websites and it’s been so fun. I’ll tell you what Flask is, show how to install Flask via command line, and how to use Flask via Python and HTML and you’ll be able to create simple pages. It honestly isn’t that hard and it won’t take that long to get a website up and running!
The definition of Flask: a Python module for writing web applications. So you need to install Flask via command line: sudo easy_install flask markdown
The next thing you have to do is create a server.py file (I would recommend cloud9 but you can choose an editor of your choice) and in the server.py file, you should have this in your server.py file to create your first page:
import os
from flask import Flask
app = Flask(__name__)
@app.route('/')
def helloWorld():
return "Hello World"
#start the server
if __name__ == '__main__':
app.run(host=os.getenv('IP', '0.0.0.0'), port=int(os.getenv('PORT', 8080)), debug=True)
If you run the server and get the link that you get from you terminal, you should get the same result as I get in this picture below:
You can in this case in your server.py file, you can use a bit of HTML code to make it more better than it being plain text like this:
import os
from flask import Flask
app = Flask(__name__)
@app.route('/')
def helloWorld():
return "<h1 style=\"color:red\">Hello World</h1>"
#start the server
if __name__ == '__main__':
app.run(host=os.getenv('IP', '0.0.0.0'), port=int(os.getenv('PORT', 8080)), debug=True)
What that will do up there will change the size of the text bigger and change the color to red. So if you did what I did above, you should get the same result as I did in this picture below:
If you wanted to make more than one page, all you have to do is with this code below:
import os
from flask import Flask
app = Flask(__name__)
@app.route('/')
def helloWorld():
return "<h1 style=\"color:red\">Hello World</h1> Welcome to my website! Click here to find about <a href=\"about\">me!</a>"
@app.route('/about')
def aboutMe():
return "<h1>About me!</h1> Favorite basketball team: Boston Celtics"
#start the server
if __name__ == '__main__':
app.run(host=os.getenv('IP', '0.0.0.0'), port=int(os.getenv('PORT', 8080)), debug=True)
This will simply create and allow you to switch to your about page you have in “@app.route('/about')
”. If you did what I did above, you should get the same results as I did in these pictures:
Then once you click on “me”, you should get this page:
If you want to put HTML code in an HTML file, but first you need to connect from within your server.py file to your HTML file by using it like this below:
<a href="/">Home<a>
<a href="/about">About me<a>
This will allow you to change pages by selecting either one of those two tabs (Home/About me).
Sources: https://code.tutsplus.com/tutorials/an-introduction-to-pythons-flask-framework–net-28822, http://flask.pocoo.org/