Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.59 KB | None | 0 0
  1. 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.
  2. main file is 1.py and working in pins.py and html file is index.html
  3. 1.py
  4. from flask import Flask, render_template, request, jsonify
  5. import Pins
  6. import sqlite3
  7.  
  8. app = Flask(__name__)
  9.  
  10. DB = "dms_resp.db"
  11. # return index page when IP address of RPi is typed in the browser
  12. @app.route("/")
  13. def Index():
  14. conn = sqlite3.connect( DB )
  15. conn.row_factory = sqlite3.Row # This enables column access by name: row['column_name']
  16. db = conn.cursor()
  17.  
  18. rows = db.execute('''
  19. SELECT id,status from seats ''').fetchall()
  20. print rows,'+++++++++++++'
  21. return render_template("index.html", uptime=GetUptime(),rows = rows, flip1='on')
  22.  
  23. # ajax GET call this function to set led state
  24. # depeding on the GET parameter sent
  25. @app.route("/_led1")
  26. def _led1():
  27. state = request.args.get('state1')
  28. if state=="on":
  29. Pins.LEDon1()
  30. else:
  31. Pins.LEDoff1()
  32. return ""
  33. @app.route("/_led2")
  34. def _led2():
  35. state = request.args.get('state2')
  36. if state=="on":
  37. Pins.LEDon2()
  38. else:
  39. Pins.LEDoff2()
  40. return ""
  41. @app.route("/_led3")
  42. def _led3():
  43. state = request.args.get('state3')
  44. if state=="on":
  45. Pins.LEDon3()
  46. else:
  47. Pins.LEDoff3()
  48. return ""
  49. @app.route("/_led4")
  50. def _led4():
  51. state = request.args.get('state4')
  52. if state=="on":
  53. Pins.LEDon4()
  54. else:
  55. Pins.LEDoff4()
  56. return ""
  57.  
  58. @app.route("/_led5")
  59. def _led5():
  60. state = request.args.get('state5')
  61. if state=="on":
  62. Pins.LEDon5()
  63. else:
  64. Pins.LEDoff5()
  65. return ""
  66.  
  67. @app.route("/_led6")
  68. def _led6():
  69. state = request.args.get('state6')
  70. if state=="on":
  71. Pins.LEDon6()
  72. else:
  73. Pins.LEDoff6()
  74. return ""
  75.  
  76. @app.route("/_led7")
  77. def _led7():
  78. state = request.args.get('state7')
  79. if state=="on":
  80. Pins.LEDon7()
  81. else:
  82. Pins.LEDoff7()
  83. return ""
  84.  
  85. @app.route("/_led8")
  86. def _led8():
  87. state = request.args.get('state8')
  88. if state=="on":
  89. Pins.LEDon8()
  90. else:
  91. Pins.LEDoff8()
  92. return ""
  93.  
  94. @app.route("/_led9")
  95. def _led9():
  96. state = request.args.get('state9')
  97. if state=="on":
  98. Pins.LEDon9()
  99. else:
  100. Pins.LEDoff9()
  101. return ""
  102.  
  103. @app.route("/_led10")
  104. def _led10():
  105. state = request.args.get('state10')
  106. if state=="on":
  107. Pins.LEDon10()
  108. else:
  109. Pins.LEDoff10()
  110. return ""
  111.  
  112. # @app.route("/_button")
  113. # def _button():
  114. # if Pins.ReadButton():
  115. # state = "Off"
  116. # else:
  117. # state = "On"
  118. #return jsonify(buttonState=state)
  119.  
  120. def GetUptime():
  121. # get uptime from the linux terminal command
  122. from subprocess import check_output
  123. output = check_output(["uptime"])
  124. # return only uptime info
  125. uptime = output[output.find("up"):output.find("user")-5]
  126. return uptime
  127.  
  128. # run the webserver on standard port 80, requires sudo
  129. if __name__ == "__main__":
  130. # Pins.Init()
  131. app.run(host='0.0.0.0', port=5000, debug=True)
  132.  
  133. pins.py
  134. from flask import Flask, render_template, request, jsonify
  135. import Pins
  136. import sqlite3
  137.  
  138. app = Flask(__name__)
  139.  
  140. DB = "dms_resp.db"
  141. # return index page when IP address of RPi is typed in the browser
  142. @app.route("/")
  143. def Index():
  144. conn = sqlite3.connect( DB )
  145. conn.row_factory = sqlite3.Row # This enables column access by name: row['column_name']
  146. db = conn.cursor()
  147.  
  148. rows = db.execute('''
  149. SELECT id,status from seats ''').fetchall()
  150. print rows,'+++++++++++++'
  151. return render_template("index.html", uptime=GetUptime(),rows = rows, flip1='on')
  152.  
  153. # ajax GET call this function to set led state
  154. # depeding on the GET parameter sent
  155. @app.route("/_led1")
  156. def _led1():
  157. state = request.args.get('state1')
  158. if state=="on":
  159. Pins.LEDon1()
  160. else:
  161. Pins.LEDoff1()
  162. return ""
  163. @app.route("/_led2")
  164. def _led2():
  165. state = request.args.get('state2')
  166. if state=="on":
  167. Pins.LEDon2()
  168. else:
  169. Pins.LEDoff2()
  170. return ""
  171. @app.route("/_led3")
  172. def _led3():
  173. state = request.args.get('state3')
  174. if state=="on":
  175. Pins.LEDon3()
  176. else:
  177. Pins.LEDoff3()
  178. return ""
  179. @app.route("/_led4")
  180. def _led4():
  181. state = request.args.get('state4')
  182. if state=="on":
  183. Pins.LEDon4()
  184. else:
  185. Pins.LEDoff4()
  186. return ""
  187.  
  188. @app.route("/_led5")
  189. def _led5():
  190. state = request.args.get('state5')
  191. if state=="on":
  192. Pins.LEDon5()
  193. else:
  194. Pins.LEDoff5()
  195. return ""
  196.  
  197. @app.route("/_led6")
  198. def _led6():
  199. state = request.args.get('state6')
  200. if state=="on":
  201. Pins.LEDon6()
  202. else:
  203. Pins.LEDoff6()
  204. return ""
  205.  
  206. @app.route("/_led7")
  207. def _led7():
  208. state = request.args.get('state7')
  209. if state=="on":
  210. Pins.LEDon7()
  211. else:
  212. Pins.LEDoff7()
  213. return ""
  214.  
  215. @app.route("/_led8")
  216. def _led8():
  217. state = request.args.get('state8')
  218. if state=="on":
  219. Pins.LEDon8()
  220. else:
  221. Pins.LEDoff8()
  222. return ""
  223.  
  224. @app.route("/_led9")
  225. def _led9():
  226. state = request.args.get('state9')
  227. if state=="on":
  228. Pins.LEDon9()
  229. else:
  230. Pins.LEDoff9()
  231. return ""
  232.  
  233. @app.route("/_led10")
  234. def _led10():
  235. state = request.args.get('state10')
  236. if state=="on":
  237. Pins.LEDon10()
  238. else:
  239. Pins.LEDoff10()
  240. return ""
  241.  
  242. # @app.route("/_button")
  243. # def _button():
  244. # if Pins.ReadButton():
  245. # state = "Off"
  246. # else:
  247. # state = "On"
  248. #return jsonify(buttonState=state)
  249.  
  250. def GetUptime():
  251. # get uptime from the linux terminal command
  252. from subprocess import check_output
  253. output = check_output(["uptime"])
  254. # return only uptime info
  255. uptime = output[output.find("up"):output.find("user")-5]
  256. return uptime
  257.  
  258. # run the webserver on standard port 80, requires sudo
  259. if __name__ == "__main__":
  260. # Pins.Init()
  261. app.run(host='0.0.0.0', port=5000, debug=True)
  262.  
  263. index.html
  264. <!doctype html>
  265. <head>
  266. <meta name="viewport" content="initial-scale=1, maximum-scale=1">
  267. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.css" />
  268. <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  269. <script src="http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.js"></script>
  270. {# <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> #}
  271. {#<script>window.jQuery || document.write('<script src="{{url_for('static', filename='jquery.js') }}">x3C/script>')</script>#}
  272. </head>
  273.  
  274. <style>
  275. h3, h4 {text-align: center;}
  276. span {font-weight: bold;}
  277. </style>
  278.  
  279. <script type=text/javascript>
  280.  
  281. // $('document').ready(function(){
  282. // alert("{{rows[0].Status}}");
  283.  
  284.  
  285. // });
  286. $(
  287. // When the LED button is pressed (change)
  288. // do an ajax request to server to change LED state
  289. function()
  290. {
  291. $('#flip-1').change(function()
  292. {
  293. // $.getJSON('/_led1', {state1: $('#flip-1').val()});
  294. // alert('change triggered !');
  295. var state1= $('#flip-1').val();
  296. var request = $.ajax({
  297. url: "/_led1",
  298. type: "GET",
  299. data: {
  300. 'state1': state1
  301. },
  302. success:function (data){
  303.  
  304. alert("{{rows[1]["status"]}}");
  305. }
  306. });
  307. // alert('change triggered !');
  308. });
  309.  
  310. $('#flip-2').change(function()
  311. {
  312. $.getJSON('/_led2', {state2: $('#flip-2').val()});
  313. });
  314.  
  315. $('#flip-3').change(function()
  316. {
  317. $.getJSON('/_led3', {state3: $('#flip-3').val()});
  318. });
  319. $('#flip-4').change(function()
  320. {
  321. $.getJSON('/_led4', {state4: $('#flip-4').val()});
  322. });
  323.  
  324. $('#flip-5').change(function()
  325. {
  326. $.getJSON('/_led5', {state5: $('#flip-5').val()});
  327. });
  328.  
  329. $('#flip-6').change(function()
  330. {
  331. $.getJSON('/_led6', {state6: $('#flip-6').val()});
  332. });
  333.  
  334. $('#flip-7').change(function()
  335. {
  336. $.getJSON('/_led7', {state7: $('#flip-7').val()});
  337. });
  338.  
  339. $('#flip-8').change(function()
  340. {
  341. $.getJSON('/_led8', {state8: $('#flip-8').val()});
  342. });
  343.  
  344. $('#flip-9').change(function()
  345. {
  346. $.getJSON('/_led9', {state9: $('#flip-9').val()});
  347. });
  348.  
  349. $('#flip-10').change(function()
  350. {
  351. $.getJSON('/_led10', {state10: $('#flip-10').val()});
  352. });
  353.  
  354. }
  355. );
  356.  
  357. $(
  358. // periodically (500ms) do an ajax request to get the button state
  359. // modify the span tag to reflect the state (pressed or not)
  360. // the state text comes from the JSON string returned by the server
  361.  
  362. );
  363.  
  364.  
  365. </script>
  366. <!-- Simple JQuery Mobile page that display the button state on the breadoard -->
  367. <!-- You can also change the LED state with the slider switch -->
  368. <!-- The Raspberry Pi uptime is displayed in the footer (Jinja2 expands the template tag) -->
  369.  
  370. <div data-role="page" data-theme="b">
  371. <div data-role="header">
  372. <div style="text-align:center;"><img src="/static/img/site_logo-1.jpeg" width="100"/><h3>Digital Marketing System Pvt. Ltd.</h3></div>
  373. </div>
  374.  
  375. <div data-role="content" style="text-align:center;">
  376. <form>
  377. <p>State <span id="buttonState"></span></p>
  378. <br>
  379. <table align="center">
  380. <tr>
  381. <td> Device 1 : </td>
  382. <td>
  383. <select name="flip-1" id="flip-1" data-role="slider" style="float: left;">
  384. <option id ="off1" value="off">OFF</option>
  385. <option id = "on1" value="on">ON</option>
  386. </select>
  387. <div></div>
  388. </td>
  389. </tr>
  390. <tr>
  391. <td> Device 2 : </td>
  392. <td>
  393. <select name="flip-2" id="flip-2" data-role="slider" style="float: left;">
  394. <option value="off">OFF</option>
  395. <option value="on">ON</option>
  396. </select>
  397. </td>
  398. </tr>
  399.  
  400. <tr>
  401. <td> Device 3 : </td>
  402. <td>
  403. <select name="flip-3" id="flip-3" data-role="slider" style="float: left;">
  404. <option value="off">OFF</option>
  405. <option value="on">ON</option>
  406. </select>
  407. </td>
  408. </tr>
  409.  
  410. <tr>
  411. <td> Device 4 : </td>
  412. <td>
  413. <select name="flip-4" id="flip-4" data-role="slider" style="float: left;">
  414. <option value="off">OFF</option>
  415. <option value="on">ON</option>
  416. </select>
  417. </td>
  418. </tr>
  419.  
  420. <tr>
  421. <td> Device 5 : </td>
  422. <td>
  423. <select name="flip-5" id="flip-5" data-role="slider" style="float: left;">
  424. <option value="off">OFF</option>
  425. <option value="on">ON</option>
  426. </select>
  427. </td>
  428. </tr>
  429.  
  430. <tr>
  431. <td> Device 6 : </td>
  432. <td>
  433. <select name="flip-6" id="flip-6" data-role="slider" style="float: left;">
  434. <option value="off">OFF</option>
  435. <option value="on">ON</option>
  436. </select>
  437. </td>
  438. </tr>
  439. <tr>
  440. <td> Device 7 : </td>
  441. <td>
  442. <select name="flip-7" id="flip-7" data-role="slider" style="float: left;">
  443. <option value="off">OFF</option>
  444. <option value="on">ON</option>
  445. </select>
  446. </td>
  447. </tr>
  448. <tr>
  449. <td> Device 8 : </td>
  450. <td>
  451. <select name="flip-8" id="flip-8" data-role="slider" style="float: left;">
  452. <option value="off">OFF</option>
  453. <option value="on">ON</option>
  454. </select>
  455. </td>
  456. </tr>
  457. <tr>
  458. <td> Device 9 : </td>
  459. <td>
  460. <select name="flip-9" id="flip-9" data-role="slider" style="float: left;">
  461. <option value="off">OFF</option>
  462. <option value="on">ON</option>
  463. </select>
  464. </td>
  465. </tr>
  466. <tr>
  467. <td> Device 10 : </td>
  468. <td>
  469. <select name="flip-10" id="flip-10" data-role="slider" style="float: left;">
  470. <option value="off">OFF</option>
  471. <option value="on">ON</option>
  472. </select>
  473. </td>
  474. </tr>
  475.  
  476. </table>
  477. </form>
  478. </div>
  479. <div data-role="footer">
  480. <div><h4>Machine has been {{uptime}},{{rows[1]["status"]}},{{rows[10]["status"]}}</h4></div>
  481. </div>
  482. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement