Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. // Tutorial Python code
  2. import os
  3. from flask import Flask, render_template
  4.  
  5. app = Flask(__name__)
  6.  
  7. @app.route("/")
  8. def index():
  9. return render_template("index.html", message = "Hello Flask!", contacts = ['c1', 'c2', 'c3', 'c4', 'c5']);
  10.  
  11.  
  12. if __name__ == "__main__":
  13. app.run()
  14.  
  15. // Corresponding Tutorial HTML
  16. <!DOCTYPE html>
  17. <html>
  18. <head>
  19. <title>Flask Template Example</title>
  20. </head>
  21.  
  22. <body>
  23. <div>
  24. <p>{{ message }}</p>
  25.  
  26. <p>{{ contacts }}</p>
  27. <p>My Contacts:</p>
  28. <ul>
  29. {% for contact in contacts %}
  30. <li>{{ contact }}</li>
  31. {% endfor %}
  32. </ul>
  33. </div>
  34. </body>
  35.  
  36. </html>
  37.  
  38. // Second tutorial Python, two @app.route
  39.  
  40. import sys
  41.  
  42. from flask import Flask, render_template, request, redirect, Response
  43. import random, json
  44.  
  45. app = Flask(__name__)
  46.  
  47. @app.route('/')
  48. def output():
  49. return render_template('index.html', name='Joe')
  50.  
  51. @app.route('/reciever', methods = ['POST'])
  52. def worker():
  53. data = request.get_json()
  54. result = ''
  55.  
  56. for item in data:
  57. result += str(item['make']) + 'n'
  58.  
  59. return result
  60.  
  61. if __name__ == "__main__":
  62. app.run()
  63.  
  64. // JavaScript communication first attempt (I was sending them to the second tutorial's Python code)
  65.  
  66. var sentArray = {"array" : [key, start, finish, holdtime, ud, uu, dd, 1]};
  67. $.post("reciever",sentArray,function(){
  68. });
  69. event.preventDefault();
  70.  
  71. // JavaScript communication second attempt
  72.  
  73. var sentArray = {"array" : [key, start, finish, holdtime, ud, uu, dd, 1]};
  74. fetch('/typing', {
  75. method: 'POST',
  76.  
  77. headers: {
  78. 'Content-Type': 'application/json'
  79. }
  80.  
  81. body:
  82. {"array" : [key, start, finish, holdtime, ud, uu, dd, 1]}
  83. }
  84. });
  85. event.preventDefault();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement