Python is an incredibly popular language for web development. If you’re interested in learning it or just want to experiment with incorporating Python into your projects, the quick-and-easy service PythonAnywhere will help get you started.
To use PythonAnywhere, all you need is a web browser and about 5 minutes of your time (and if you’re on a slow internet connection, maybe some patience as well). Just follow these simple steps:
Go to PythonAnywhere.
Sign up for a free account and fill out your details.
Then click on “open web apps”.
Then click on “add a new web app”.
You’ll see a page that lets you configure your app:
This page allows you to configure the Web framework you want to use. There are many different frameworks like Django, web2py, Flask, Bottle and others.
For simplicity, I’ll go with Flask.
Then it lets you select your Python version. Go for the newest version.
Finally enter a Path for your Python file and click next. That sets up your Python web app and it is now hosted and online!
You can then open the source for of your web app. Either through the bash console or the web interface. They provide an online editor.
By creating a web app, they automatically created a template for the Flask web app. The template looks like this:
# A very simple Flask Hello World app for you to get started with...
from flask import Flask
= Flask(__name__)
app
@app.route('/')
def hello_world():
return 'Hello from Flask!'
The first line of code simply loads the Flask module.
from flask import Flask
Then the app is created
= Flask(__name__) app
Then a route is created. A route is mapping of a link to a Python function. In the case the / link is called, Python will execute the function hello_world(). The return value is shown in the browser.
@app.route('/')
def hello_world():
return 'Hello from Flask!'
You can change this value to anything you want. If you return html tags, the browser will simply render html.
You can add as many routes as you want. Every web url has the map to a unique Python function, for example try this:
@app.route('/')
def hello_world():
return 'Hello from Flask!'
@app.route('/html')
def html():
return '<h1>html</h1>'
@app.route('/image')
def image():
return "<img src='https://wallup.net/wp-content/uploads/2019/10/355155-cat-meme-quote-funny-humor-grumpy-computer.jpg">'
Of course there’s a lot more you can do with the Python Flask framework, these are just the absolute basics.
A url route is a mapping between an HTTP verb and an endpoint. The endpoint can be a resource, such as a file or script, or it can be a function that processes the request.
In Flask, routes are defined in the app object. Each route is associated with a handler, which is a Python function that takes an incoming request and produces an outgoing response.
@app.route('/url')
def a_function():
return 'Data shown in web browser!'
The first argument to the route decorator is the path of the URL. This can be a static string, or it can contain variable parts enclosed in angle brackets (<>). Variable parts are converted to positional arguments