Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- my page is resetting from start after refreshing the page so it is not staying with current database values so how could i do this.
- main file is 1.py and working in pins.py and html file is index.html
- 1.py
- from flask import Flask, render_template, request, jsonify
- import Pins
- import sqlite3
- app = Flask(__name__)
- DB = "dms_resp.db"
- # return index page when IP address of RPi is typed in the browser
- @app.route("/")
- def Index():
- conn = sqlite3.connect( DB )
- conn.row_factory = sqlite3.Row # This enables column access by name: row['column_name']
- db = conn.cursor()
- rows = db.execute('''
- SELECT id,status from seats ''').fetchall()
- print rows,'+++++++++++++'
- return render_template("index.html", uptime=GetUptime(),rows = rows, flip1='on')
- # ajax GET call this function to set led state
- # depeding on the GET parameter sent
- @app.route("/_led1")
- def _led1():
- state = request.args.get('state1')
- if state=="on":
- Pins.LEDon1()
- else:
- Pins.LEDoff1()
- return ""
- @app.route("/_led2")
- def _led2():
- state = request.args.get('state2')
- if state=="on":
- Pins.LEDon2()
- else:
- Pins.LEDoff2()
- return ""
- @app.route("/_led3")
- def _led3():
- state = request.args.get('state3')
- if state=="on":
- Pins.LEDon3()
- else:
- Pins.LEDoff3()
- return ""
- @app.route("/_led4")
- def _led4():
- state = request.args.get('state4')
- if state=="on":
- Pins.LEDon4()
- else:
- Pins.LEDoff4()
- return ""
- @app.route("/_led5")
- def _led5():
- state = request.args.get('state5')
- if state=="on":
- Pins.LEDon5()
- else:
- Pins.LEDoff5()
- return ""
- @app.route("/_led6")
- def _led6():
- state = request.args.get('state6')
- if state=="on":
- Pins.LEDon6()
- else:
- Pins.LEDoff6()
- return ""
- @app.route("/_led7")
- def _led7():
- state = request.args.get('state7')
- if state=="on":
- Pins.LEDon7()
- else:
- Pins.LEDoff7()
- return ""
- @app.route("/_led8")
- def _led8():
- state = request.args.get('state8')
- if state=="on":
- Pins.LEDon8()
- else:
- Pins.LEDoff8()
- return ""
- @app.route("/_led9")
- def _led9():
- state = request.args.get('state9')
- if state=="on":
- Pins.LEDon9()
- else:
- Pins.LEDoff9()
- return ""
- @app.route("/_led10")
- def _led10():
- state = request.args.get('state10')
- if state=="on":
- Pins.LEDon10()
- else:
- Pins.LEDoff10()
- return ""
- # @app.route("/_button")
- # def _button():
- # if Pins.ReadButton():
- # state = "Off"
- # else:
- # state = "On"
- #return jsonify(buttonState=state)
- def GetUptime():
- # get uptime from the linux terminal command
- from subprocess import check_output
- output = check_output(["uptime"])
- # return only uptime info
- uptime = output[output.find("up"):output.find("user")-5]
- return uptime
- # run the webserver on standard port 80, requires sudo
- if __name__ == "__main__":
- # Pins.Init()
- app.run(host='0.0.0.0', port=5000, debug=True)
- pins.py
- from flask import Flask, render_template, request, jsonify
- import Pins
- import sqlite3
- app = Flask(__name__)
- DB = "dms_resp.db"
- # return index page when IP address of RPi is typed in the browser
- @app.route("/")
- def Index():
- conn = sqlite3.connect( DB )
- conn.row_factory = sqlite3.Row # This enables column access by name: row['column_name']
- db = conn.cursor()
- rows = db.execute('''
- SELECT id,status from seats ''').fetchall()
- print rows,'+++++++++++++'
- return render_template("index.html", uptime=GetUptime(),rows = rows, flip1='on')
- # ajax GET call this function to set led state
- # depeding on the GET parameter sent
- @app.route("/_led1")
- def _led1():
- state = request.args.get('state1')
- if state=="on":
- Pins.LEDon1()
- else:
- Pins.LEDoff1()
- return ""
- @app.route("/_led2")
- def _led2():
- state = request.args.get('state2')
- if state=="on":
- Pins.LEDon2()
- else:
- Pins.LEDoff2()
- return ""
- @app.route("/_led3")
- def _led3():
- state = request.args.get('state3')
- if state=="on":
- Pins.LEDon3()
- else:
- Pins.LEDoff3()
- return ""
- @app.route("/_led4")
- def _led4():
- state = request.args.get('state4')
- if state=="on":
- Pins.LEDon4()
- else:
- Pins.LEDoff4()
- return ""
- @app.route("/_led5")
- def _led5():
- state = request.args.get('state5')
- if state=="on":
- Pins.LEDon5()
- else:
- Pins.LEDoff5()
- return ""
- @app.route("/_led6")
- def _led6():
- state = request.args.get('state6')
- if state=="on":
- Pins.LEDon6()
- else:
- Pins.LEDoff6()
- return ""
- @app.route("/_led7")
- def _led7():
- state = request.args.get('state7')
- if state=="on":
- Pins.LEDon7()
- else:
- Pins.LEDoff7()
- return ""
- @app.route("/_led8")
- def _led8():
- state = request.args.get('state8')
- if state=="on":
- Pins.LEDon8()
- else:
- Pins.LEDoff8()
- return ""
- @app.route("/_led9")
- def _led9():
- state = request.args.get('state9')
- if state=="on":
- Pins.LEDon9()
- else:
- Pins.LEDoff9()
- return ""
- @app.route("/_led10")
- def _led10():
- state = request.args.get('state10')
- if state=="on":
- Pins.LEDon10()
- else:
- Pins.LEDoff10()
- return ""
- # @app.route("/_button")
- # def _button():
- # if Pins.ReadButton():
- # state = "Off"
- # else:
- # state = "On"
- #return jsonify(buttonState=state)
- def GetUptime():
- # get uptime from the linux terminal command
- from subprocess import check_output
- output = check_output(["uptime"])
- # return only uptime info
- uptime = output[output.find("up"):output.find("user")-5]
- return uptime
- # run the webserver on standard port 80, requires sudo
- if __name__ == "__main__":
- # Pins.Init()
- app.run(host='0.0.0.0', port=5000, debug=True)
- index.html
- <!doctype html>
- <head>
- <meta name="viewport" content="initial-scale=1, maximum-scale=1">
- <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.css" />
- <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
- <script src="http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.js"></script>
- {# <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> #}
- {#<script>window.jQuery || document.write('<script src="{{url_for('static', filename='jquery.js') }}">x3C/script>')</script>#}
- </head>
- <style>
- h3, h4 {text-align: center;}
- span {font-weight: bold;}
- </style>
- <script type=text/javascript>
- // $('document').ready(function(){
- // alert("{{rows[0].Status}}");
- // });
- $(
- // When the LED button is pressed (change)
- // do an ajax request to server to change LED state
- function()
- {
- $('#flip-1').change(function()
- {
- // $.getJSON('/_led1', {state1: $('#flip-1').val()});
- // alert('change triggered !');
- var state1= $('#flip-1').val();
- var request = $.ajax({
- url: "/_led1",
- type: "GET",
- data: {
- 'state1': state1
- },
- success:function (data){
- alert("{{rows[1]["status"]}}");
- }
- });
- // alert('change triggered !');
- });
- $('#flip-2').change(function()
- {
- $.getJSON('/_led2', {state2: $('#flip-2').val()});
- });
- $('#flip-3').change(function()
- {
- $.getJSON('/_led3', {state3: $('#flip-3').val()});
- });
- $('#flip-4').change(function()
- {
- $.getJSON('/_led4', {state4: $('#flip-4').val()});
- });
- $('#flip-5').change(function()
- {
- $.getJSON('/_led5', {state5: $('#flip-5').val()});
- });
- $('#flip-6').change(function()
- {
- $.getJSON('/_led6', {state6: $('#flip-6').val()});
- });
- $('#flip-7').change(function()
- {
- $.getJSON('/_led7', {state7: $('#flip-7').val()});
- });
- $('#flip-8').change(function()
- {
- $.getJSON('/_led8', {state8: $('#flip-8').val()});
- });
- $('#flip-9').change(function()
- {
- $.getJSON('/_led9', {state9: $('#flip-9').val()});
- });
- $('#flip-10').change(function()
- {
- $.getJSON('/_led10', {state10: $('#flip-10').val()});
- });
- }
- );
- $(
- // periodically (500ms) do an ajax request to get the button state
- // modify the span tag to reflect the state (pressed or not)
- // the state text comes from the JSON string returned by the server
- );
- </script>
- <!-- Simple JQuery Mobile page that display the button state on the breadoard -->
- <!-- You can also change the LED state with the slider switch -->
- <!-- The Raspberry Pi uptime is displayed in the footer (Jinja2 expands the template tag) -->
- <div data-role="page" data-theme="b">
- <div data-role="header">
- <div style="text-align:center;"><img src="/static/img/site_logo-1.jpeg" width="100"/><h3>Digital Marketing System Pvt. Ltd.</h3></div>
- </div>
- <div data-role="content" style="text-align:center;">
- <form>
- <p>State <span id="buttonState"></span></p>
- <br>
- <table align="center">
- <tr>
- <td> Device 1 : </td>
- <td>
- <select name="flip-1" id="flip-1" data-role="slider" style="float: left;">
- <option id ="off1" value="off">OFF</option>
- <option id = "on1" value="on">ON</option>
- </select>
- <div></div>
- </td>
- </tr>
- <tr>
- <td> Device 2 : </td>
- <td>
- <select name="flip-2" id="flip-2" data-role="slider" style="float: left;">
- <option value="off">OFF</option>
- <option value="on">ON</option>
- </select>
- </td>
- </tr>
- <tr>
- <td> Device 3 : </td>
- <td>
- <select name="flip-3" id="flip-3" data-role="slider" style="float: left;">
- <option value="off">OFF</option>
- <option value="on">ON</option>
- </select>
- </td>
- </tr>
- <tr>
- <td> Device 4 : </td>
- <td>
- <select name="flip-4" id="flip-4" data-role="slider" style="float: left;">
- <option value="off">OFF</option>
- <option value="on">ON</option>
- </select>
- </td>
- </tr>
- <tr>
- <td> Device 5 : </td>
- <td>
- <select name="flip-5" id="flip-5" data-role="slider" style="float: left;">
- <option value="off">OFF</option>
- <option value="on">ON</option>
- </select>
- </td>
- </tr>
- <tr>
- <td> Device 6 : </td>
- <td>
- <select name="flip-6" id="flip-6" data-role="slider" style="float: left;">
- <option value="off">OFF</option>
- <option value="on">ON</option>
- </select>
- </td>
- </tr>
- <tr>
- <td> Device 7 : </td>
- <td>
- <select name="flip-7" id="flip-7" data-role="slider" style="float: left;">
- <option value="off">OFF</option>
- <option value="on">ON</option>
- </select>
- </td>
- </tr>
- <tr>
- <td> Device 8 : </td>
- <td>
- <select name="flip-8" id="flip-8" data-role="slider" style="float: left;">
- <option value="off">OFF</option>
- <option value="on">ON</option>
- </select>
- </td>
- </tr>
- <tr>
- <td> Device 9 : </td>
- <td>
- <select name="flip-9" id="flip-9" data-role="slider" style="float: left;">
- <option value="off">OFF</option>
- <option value="on">ON</option>
- </select>
- </td>
- </tr>
- <tr>
- <td> Device 10 : </td>
- <td>
- <select name="flip-10" id="flip-10" data-role="slider" style="float: left;">
- <option value="off">OFF</option>
- <option value="on">ON</option>
- </select>
- </td>
- </tr>
- </table>
- </form>
- </div>
- <div data-role="footer">
- <div><h4>Machine has been {{uptime}},{{rows[1]["status"]}},{{rows[10]["status"]}}</h4></div>
- </div>
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement