Python Forum
Flask: How to make this web page not reloading?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flask: How to make this web page not reloading?
#1
Hello, I am new to this forum, and I have searched any similar question but I didn't find one.
I have made a web page using Flask for connecting to Raspberry Pi. It works fine, actually, but I don't like that this web page always reloads every time I click a link. Can somebody teach me how to make this web page not reloading? Thank you.

from flask import Flask
import RPi.GPIO as GPIO
import collections
GPIO.setwarnings(False)

pins = [17, 18, 27, 22]

pin_led_states = [
    [1, 0, -1, -1],  # 1
    [0, 1, -1, -1],  # 2
    [-1, 1, 0, -1],  # 3
    [-1, 0, 1, -1],  # 4
    [1, -1, 0, -1],  # 5
    [0, -1, 1, -1],  # 6
    [1, -1, -1, 0],  # 7
    [0, -1, -1, 1],  # 8
    [-1, 1, -1, 0],  # 9
    [-1, 0, -1, 1],  # 10
    [-1, -1, 1, 0],  # 11
    [-1, -1, 0, 1]   # 12
]

GPIO.setmode(GPIO.BCM)

def set_pin(pin_index, pin_state):
    if pin_state == -1:
        GPIO.setup(pins[pin_index], GPIO.IN)
    else:
        GPIO.setup(pins[pin_index], GPIO.OUT)
        GPIO.output(pins[pin_index], pin_state)

def light_led(led_number):
    for pin_index, pin_state in enumerate(pin_led_states[led_number]):
        set_pin(pin_index, pin_state)
    led_on=led_number    

def led_off():
    set_pin(0, -1)
    set_pin(1, -1)
    set_pin(2, -1)
    set_pin(3, -1)

def find_list_in_list(inputlist, the_list):
    # Flag initialization
    n = -1
    counter=0
    # Using Counter
    for elem in the_list:
        print(elem)
        if collections.Counter(elem) == collections.Counter(inputlist) :
            n=counter
        counter=counter+1    
    return n   

def check_light():
    state=[]
    for n in range(4):
        state.append(GPIO.input(pins[n]))
    return find_list_in_list(state, pin_led_states)

def webpage(led_on):
    a=''
    for n in range(12):
        ledprefix='off'
        if n==led_on:
            ledprefix='on'
        if ((n+1)==1) or ((n+1)==2) or ((n+1)==7) or ((n+1)==8):
            a='{}               <div style="width:100px; height:100px; padding: 10px; float:left"><a href="/{}"><img src="/static/green-led-{}-100px.png" style="border:0px;width:100px;height:100px"></a></div>\n'.format(a,n,ledprefix)
        elif ((n+1)==3) or ((n+1)==4) or ((n+1)==9) or ((n+1)==10):
            a='{}               <div style="width:100px; height:100px; padding: 10px; float:left"><a href="/{}"><img src="/static/blue-led-{}-100px.png" style="border:0px;width:100px;height:100px"></a></div>\n'.format(a,n,ledprefix)
        else:
            a='{}               <div style="width:100px; height:100px; padding: 10px; float:left"><a href="/{}"><img src="/static/red-led-{}-100px.png" style="border:0px;width:100px;height:100px"></a></div>\n'.format(a,n,ledprefix)
        
    return '''<!DOCTYPE html>
<html>
    <head>
        <title>Raspberry Pi Led Interface</title>
    </head>
    <body style="background-color: black; color:white">
        <div style="width:100%">
            <div style="width:100%;text-align:center"><H1>Click to Turn On LED</H1></div>
            <div style="width:240px;height:720px;margin:auto">
{}            </div>
            <div style="width:100%; margin-top:20px; text-align:center; font-size: 20pt;"><a href="/turnoff_all" style="color:white">Turn off all LEDs</a></div>
        </div>
    </body>
</html>'''.format(a)

led_off()

app = Flask(__name__)

@app.route('/')
def home():
    return webpage(-1)
            
@app.route('/<int:ledno>')
def led_no(ledno):
    light_led(ledno)
    return webpage(ledno)

@app.route('/turnoff_all')
def turnoff_all_led():
    led_off()
    return webpage(-1)


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


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to use a variable from a flask page to another Need_Not 0 1,395 Jun-03-2020, 03:59 PM
Last Post: Need_Not
  How to avoid reloading modules in python Antonio 5 21,002 May-04-2018, 09:21 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020