Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. from flask import Flask, render_template, request, session as flask_session, g, send_from_directory, redirect, url_for
  2. #from flask.ext.socketio import SocketIO, emit, send
  3. from flask_socketio import SocketIO, emit, send
  4. import model
  5. import os
  6. import datetime
  7.  
  8. UPLOAD_FOLDER = 'static/dataset'
  9. app = Flask(__name__, static_folder='static', static_url_path='/static')
  10. app.debug = True
  11. app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  12.  
  13. # should probably hide this secret key at some point?
  14. app.config['SECRET_KEY'] = 'TROLOLOLOLOLO!'
  15. socketio = SocketIO(app)
  16.  
  17. @app.before_request
  18. def global_variables():
  19. if "user" in flask_session:
  20. g.user_id = flask_session["user"]["id"]
  21. g.username = flask_session["user"]["username"]
  22. #return 'OK'
  23.  
  24. @app.route('/', methods=['GET', 'POST'])
  25. def sign_up_log_in():
  26. if request.method == 'GET':
  27. return render_template('index.html')
  28.  
  29.  
  30. @app.route('/hasil', methods=['GET'])
  31. def tes():
  32. if request.method == 'GET':
  33. import TestingMaxPooling
  34. return render_template('hasil.html')
  35.  
  36.  
  37. @app.route('/Start', methods=['GET'])
  38. def BackCanvas():
  39. app.run(debug=True)
  40. return redirect(url_for('sign_up_log_in'))
  41.  
  42.  
  43.  
  44.  
  45. @app.route('/save', methods=['POST'])
  46. def save_image():
  47. img = request.files['image']
  48. if img:
  49. # mac version below
  50. # filename = g.username + ".jpg"
  51. # fullpath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
  52. # Windows version below -- unsure why there is a difference between mac/windows
  53. dateObj = datetime.datetime.now().strftime("%Y-%m-%d_%H%M%S")
  54. fullpath = app.config['UPLOAD_FOLDER'] + "/" + dateObj + ".jpg"
  55. img.save(fullpath)
  56. image = model.save_image_to_db(fullpath)
  57. #return TestingMaxPooling
  58. return "Failure"
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. @socketio.on('broadcastImage')
  67. def broadcast_image(data):
  68. emit('loadImage', data, broadcast=True)
  69.  
  70. @socketio.on('reset')
  71. def reset(data):
  72. print("Reset")
  73. emit('resetCanvas', data, broadcast=True)
  74.  
  75. # Offers the load() javascript function the path it needs
  76. @app.route('/static/img/<path:path>')
  77. def send_user_image(path):
  78. return send_from_directory('static/img', path)
  79.  
  80. @socketio.on('connection')
  81. def listen_send_all(data):
  82. emit('new user')
  83.  
  84. @socketio.on('mousemove')
  85. def brdcast_moving(data):
  86. emit('moving', data, broadcast=True)
  87.  
  88. @socketio.on('mouseup')
  89. def brdcast_stop(data):
  90. emit('stopping', data, broadcast=True)
  91.  
  92. @socketio.on('broadcastColor')
  93. def brdcast_color(data):
  94. emit('strokeColor', data, broadcast=True)
  95.  
  96. @socketio.on('deleteUnloaded')
  97. def delete_unloaded(data):
  98. emit('deleteRemoteUser', data, broadcast=True)
  99.  
  100. PORT=int(os.environ.get("PORT", 5000))
  101.  
  102. if __name__ == '__main__':
  103. socketio.run(app, host='0.0.0.0', port=PORT)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement