I'm struggling with importing the variables from my Python Script (pulsecount.py) into my Flask App (routes.py). If I simply do it like the following script, the code is executed, but it only shows the print outputs from the While Loop in pulsecount.py in the terminal and the Flask app won't run. If I delete the While Loop, the Flask App is working but I can't get the updated variable value, which in this case is conValue.

I want to show the newest output of the Raspberry Pi on the Browser, how do I do that?


import RPi.GPIO as GPIO
from time import sleep

GPIO.setup(27, GPIO.IN)

global revcount
global coinValue
global Balance

revcount = 0
coinValue = float(0.00)
Balance = float(0.00)

def increaserev(self):
    global revcount
    global coinValue
    global Balance

    revcount += 1

    if revcount == 2:
        coinValue = float(1.00)

        coinValue = float(0.5)
    return coinValue

GPIO.add_event_detect(27, GPIO.RISING, callback=increaserev)
while True:
    if revcount > 0:
        Balance += coinValue
        print "Coin Value is {0}".format(coinValue)
        print "Balance is {0}".format(Balance)

        revcount = 0
        coinValue = 0


from flask import Flask
from flask import render_template
from pulsecount import coinValue

app = Flask(__name__)

def home():
    return render_template("home.html")

def getbitcoin():

    coinvalue = coinValue

    return render_template("getbitcoin.html", coinvalue = coinvalue)

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


{% extends "template.html" %}

{% block head %}

    <script type="text/javascript">

                }, 2000);


{% endblock %}

{% block content %}

        <a id="link" href="/total/">
            <div class="get-bitcoin"> 
              <div class="get-bitcoin-button">
                <h1 class="bitcoin-header">{{ coinvalue }}</h1>
                <!-- <h1 id="result"></h1> -->

{% endblock content %}

You need to add a condition in that While loop to let it break and then process your Flask code. Currently, its just going to spin in there forever and never complete to allow the flask route method to end and return the result to the client.

You might want to consider pushing the while loop into a method and having the pulsecount start that as on a thread via

add to top
import threading

modify body with

def do_work():
    global revcount
    global coinValue
    global Balance

    while True:
        if revcount > 0:
            Balance += coinValue
            print "Coin Value is {0}".format(coinValue)
            print "Balance is {0}".format(Balance)

            revcount = 0
            coinValue = 0
        sleep(0.2) #stop it thrashing a bit

worker = threading.Thread(target=do_work, name='myworker', daemon=True)

this should let the whole thing spin along working in the background waiting for whatever that GPIO event is and when anyone asks for the coinvalue it'll be available. Bear in mind this introduces threading which is hard.

You'll also need to adjust the import of the module

import pulsecount

then change the coinvalue = coinValue to coinValue = pulsecount.coinValue otherwise the value will only be imported the once.

  • Thank you! The App is now running and the Input also shows up in the terminal. But I still can't manage to get the coinValue over to the Flask App to show up in the Browser. Do you have any Idea what might be the cause? – Hendrik Apr 11 '17 at 14:00
  • your loop is always setting the coinValue back to 0 by the looks of things? – tobyd Apr 11 '17 at 14:02
  • It behaves in the same manner if I comment it out :/ By the way I had to add the global variables in the method, otherwise I would get an Referenced before Assignment Error. – Hendrik Apr 11 '17 at 14:18
  • ah yes, missed that - I'm not exactly sure what this app is meant to do, but if you see my edit re: coinValue that should resolve that - I think it'll still be 0 due to the while loop resetting the value though? – tobyd Apr 11 '17 at 14:38
  • Yes it's working! Thank you very much for taking your time to help me! :) – Hendrik Apr 11 '17 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.