TeCoEd (Teaching Computing Education)
  • Home
    • Freelance
    • Book
    • Downloading
  • Python
    • Learn Python >
      • Python Modules
    • PyGame Zero
    • Python Programs >
      • Higher or Lower
      • Magic Calculator
      • Password Checker
      • Python Pit
    • What's News App
    • Pixels to Cells
    • Python Mosaics
    • Python OCR
    • L-1-AM
    • Web Scraping >
      • Scraping Trains
    • Weather App
    • Snakes and Windows
    • Python Web Server >
      • Flask
    • Python Picks
  • Raspberry Pi
    • All About the Pi
    • Getting Started
    • Remote Desktop and VNC
    • Static IP Address
    • Sonic Pi >
      • 3.14
    • Twitter Feed >
      • Tweepy
    • Android & Pi >
      • Advanced Apps
      • Odds
    • A.I on the the Pi
    • CRON
    • Pick Your Own
  • Pi Hardware
    • Pi HATS >
      • Sense Hat Hacks
      • AstroPi HAT
      • Unicorn-HAT >
        • Unicorn Alphabet Disco
        • Uni Codes / Programs
      • Skywriter
      • Piano HAT
    • STS Pi
    • Pi Camera >
      • Pi-Cam, Python & Email >
        • Time Lapse
      • Pi Noir
    • Pipsta >
      • Flask, Input & Printers
    • Raspberry Pi Power >
      • Energenie IR power
    • Pibrella
    • Distance Sensor
    • LCD Screen
    • Pi-Tooth
    • Robot Arm
    • PiGlow
    • PiFM
    • Accelerometer
    • PiFace >
      • Installing PiFace >
        • Python Commands
  • Pi-Hacks
    • Drone Hacks
    • Pi Glue Gun Hack
    • Blinkt!
    • Sonic Pixels
    • R2D2
    • Get to the chopper
    • Astro Bird
    • Twitter Translator
    • Hacking a Robot
    • Nature_Box >
      • Best Nature Photos
    • Wearable Tech >
      • Project New York
      • P.N.Y Part 2 Health
      • P.N.Y Part 3 Games
      • P.N.Y Part 4 Translation
    • Dino-Tweet
    • Other Links
  • Pi-Hacks 2
    • The Joker
    • Hologram Machine
    • Google Vision: Camera Tell
    • Yoda Tweets
    • Pi Phone
    • Darth Beats
    • Twitter Keyword Finder
    • Crimbo Lights Hack
    • Xmas Elf
    • Halloween 2016
    • Halloween Hack 2015
    • Socrative Zombie
    • Voice Translation
    • The Blue-Who Finder
    • GPIO, Twitter
    • Pi Chat Bot >
      • Dictionary Definitions
    • PiGlow & Email
    • Pibrella Alarm System
    • SMS with Python >
      • Spooking a Mobile
  • Pi-Hacks 3
    • David Bowie
    • Lamp Prank >
      • TEST
    • Yoda FM
    • Retro Player
    • LED Pixel Art
    • TARDIS
    • Battleships
    • LED Board
    • Night Vision
    • Enviro+ Weather
  • Minecraft
    • Minecraft API
    • Minecraft Sweeper
    • PiGlove: Minecraft Power Up
    • Minecraft Photo-booth
    • Rendering Pixels
    • Speed Cube
    • Lucky Dip
  • Computing
    • Why Computing?
    • Can You Compute
    • micro:bit
    • Coding Resources
    • Learn to Code >
      • Coding with iPads
      • Apps Creation Tools
      • sKratchInn
      • Sound Editing
    • Cheat Sheets
    • Theory
    • HOUR OF CODING
    • BEBRAS Computing Challange
    • Computer Facts
    • Free Software and Links
  • Contact Me

What is it?


Flask is a simple Python library that allows you to set up and create a dynamic web server directly on the Raspberry Pi.  A web server stores webpages of information.  When you access a website via the www address you are in fact accessing a web server that is hosting (holding) the data.   Flask can be combined with Python code host forms, databases, webpages and much more.

Installing Flask


To install Flask you may first need to install , in the LX Terminal:
sudo apt-get install python-pip

Once installed use pip to then install Flask
sudo pip install flask.

Now Flask is installed and you are ready to create your first dynamic webpage and host it via your Pi Server

Getting Started


This very simple app will allow you to display a message via the web browser on any device connected to the network. (WiFi or cable)

Open IDLE and type the following code:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "
enter_your _message_choice_here"

if __name__ == "__main__":
    app.run()


This creates a simple code that will display the messages every time the Raspberry Pi is accessed via its IP Address.  It is useful to add a debugging code line and access  to the Raspberry Pi Server outside the network, this is achieved by adding the following code to the end of the program.  This enables the program to listen for error messages and reports whenever the site is accessed. 

Modify the Python Code:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "enter_your _message_choice_here"

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


Then save the code as a .py file with a suitable name such as message.py

Accessing the RasPi Server


The next step to accessing the message is to find the IP address of your Raspberry Pi which you will connect to.  

In the LX Terminal type:
sudo ifconfig

Note the IP Address and start the Flask application.
Picture
In the LX Terminal type:
sudo python name_of_your-file.py

On you other device open the web browser and in the address bar type the IP Address of your Raspberry Pi and you should see the message that was coded into your Flask app is shown.
The message that you wrote in the return section of the definition will not display on your browser.  You Pi may return a message to state that the server has been accessed and there are no errors.
Using Flask to take user input on a website and print it
Copyright 2020 TeCoEd @dan_aldred