Build a Python Web Server with Flask: Difference between revisions

From Sensors in Schools
Jump to navigation Jump to search
No edit summary
No edit summary
Line 9: Line 9:
* Make a new directory named webapp using the Terminal command '''mkdir webapp'''
* Make a new directory named webapp using the Terminal command '''mkdir webapp'''
* Enter the new directory with the command '''cd webapp'''
* Enter the new directory with the command '''cd webapp'''
[[File:Screenshot 2023-07-22 at 6.56.59 am.png | 900px]]
= First web application ==
* Open up Thonny.
* Paste the following code into Thonny.
* Save the file as '''app.py''' in the webapp directory.
[[File:Screenshot 2023-07-22 at 7.04.47 am.png | 900px]]
<syntaxhighlight lang="python">
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
    return 'Hello world'
if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')
</syntaxhighlight>

Revision as of 21:07, 21 July 2023

Build a Python Web Server using Flask

Install Flask

  • Open the Terminal
  • Enter the command pip3 install flask

  • Make a new directory named webapp using the Terminal command mkdir webapp
  • Enter the new directory with the command cd webapp

First web application =

  • Open up Thonny.
  • Paste the following code into Thonny.
  • Save the file as app.py in the webapp directory.

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello world'

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')