I'm trying to read a variable from one script into another script. Both scripts are in the same folder. In the script where I read the variable I only get the initial value of that variable (0x00). How I can update the variables across those scripts so I can read the variables from script1 in script2?
Below script1
from flask import Flask, render_template, request, redirect
#Define variables #(This are the values i read in another script and they never update)
byte_1 = 0x00
byte_2 = 0x00
byte_3 = 0x00
byte_4 = 0x00
app = Flask(__name__)
@app.route('/', methods=["GET", "POST"])
def home():
#Make the variables global
global byte_1
global byte_2
global byte_3
global byte_4
if request.method == 'GET':
return render_template('home.html')
if request.method == 'POST':
for i, value in request.form.items():
buttons(i, value) #Call function to update the variables
return redirect(request.url) #Redirect to refresh page
def buttons(i, value):
#Make the variables global
global byte_1
global byte_2
global byte_3
global byte_4
#Compare "i" and "value" from "request.form.items" and modify variable byte_1 to byte_4
#This are the values i want to read from another script2 but i only read 0x00?
if i == ("K01") and value ==("On"): #Set bit (Bitwise operation: or)
byte_4 |= 0b10000000
if i == ("K01") and value ==("Off"): #Reset bit (Bitwise operation: and)
byte_4 &= 0b01111111
#/// and so on
#Run server
if __name__ == "__main__":
app.run(host='192.168.1.117', port=5000, debug=True)
Below is script 2. Here I want to read variables byte_1 to byte_4 from script1 but only read 0x00:
import script1
while True:
print(script1.byte_1)
print(script1.byte_2)
print(script1.byte_3)
print(script1.byte_4)