views.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from app import app
  2. from flask import request, redirect, url_for, send_from_directory, render_template
  3. import subprocess
  4. import redis
  5. import random
  6. import json
  7. import os
  8. #https://medium.com/@emerico/convert-pdf-to-image-using-python-flask-2864fb655e01
  9. #UPLOAD_FOLDER = '/Users/beatescheibel/Desktop/flask/uploads'
  10. UPLOAD_FOLDER = '/home/bscheibel/uploads_app'
  11. app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
  12. ALLOWED_EXTENSIONS = set(['pdf', 'png', 'jpg', 'jpeg', 'PDF'])
  13. def allowed_file(filename):
  14. return '.' in filename and \
  15. filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
  16. def convert_pdf_img(filename):
  17. PDFFILE = UPLOAD_FOLDER +"/" + filename
  18. subprocess.call(['pdftoppm', '-jpeg', '-singlefile',
  19. PDFFILE, '/home/bscheibel/uploads_app/out'])
  20. def extract_all(uuid, filename, db):
  21. #order_bounding_boxes_in_each_block.main(uuid, UPLOAD_FOLDER + "/" + filename)
  22. subprocess.call(['python3','/home/bscheibel/PycharmProjects/dxf_reader/main.py', str(uuid),UPLOAD_FOLDER + "/" + filename,db])
  23. @app.route('/', methods=['GET', 'POST'])
  24. def upload_file():
  25. if request.method == 'POST':
  26. file = request.files['file']
  27. if file and allowed_file(file.filename):
  28. filename = file.filename
  29. file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
  30. uuid = random.randint(100,10000000)
  31. extract_all(uuid, filename, 'localhost')
  32. return redirect(url_for('uploaded_file', filename=filename, uuid=uuid))
  33. return '''
  34. <!doctype html>
  35. <title>Upload new File</title>
  36. <h1>Upload new File</h1>
  37. <form action="" method=post enctype=multipart/form-data>
  38. <p><input type=file name=file>
  39. <input type=submit value=Upload>
  40. </form>
  41. '''
  42. @app.route('/show/<filename>&<uuid>')
  43. def uploaded_file(filename, uuid):
  44. file_out = "out.jpg"
  45. if request.method == 'POST':
  46. uuid = 433
  47. if filename.endswith(".pdf") or filename.endswith(".PDF"):
  48. convert_pdf_img(filename)
  49. db = redis.Redis("localhost")
  50. #isos = db.get(uuid+"dims")
  51. #print(iso)
  52. isos = json.loads(db.get(uuid+"isos"))
  53. dims = json.loads(db.get(uuid+"dims"))
  54. html_code = ""
  55. # dims = eval(dims)
  56. for dim in dims:
  57. html_code += '''<td><h4>''' + dim + '''</h4></td>'''
  58. for d in dims[dim]:
  59. html_code += "<tr><td style='text-align:center'> <input type='checkbox' name='relevant." + d + "' value='checked' onchange='submit()'> </td>" + \
  60. "<td style='text-align:center'>" + d + "</td>" + \
  61. "<td style='text-align:center'> <input type='number' name='" + d + "' value='" + d + "' size='10' onchange='submit()'> </td></tr>"
  62. print(html_code)
  63. return render_template('show_image.html', filename=file_out, isos=isos, dims=dims, text=html_code)
  64. else:
  65. filename = filename
  66. return render_template('show_image.html', filename=filename)
  67. @app.route('/uploads/<filename>')
  68. def send_file(filename):
  69. return send_from_directory(UPLOAD_FOLDER, filename)
  70. # No caching at all for API endpoints.
  71. @app.after_request
  72. def add_header(response):
  73. # response.cache_control.no_store = True
  74. response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
  75. response.headers['Pragma'] = 'no-cache'
  76. response.headers['Expires'] = '-1'
  77. return response
  78. @app.route('/generate/<name>')
  79. def generate(name):
  80. name = name.replace(" ","")
  81. url = name+".PDF"
  82. try:
  83. file = send_from_directory("static/isos",url)
  84. return file
  85. except:
  86. return"Sorry file not found"
  87. @app.route('/show_results')
  88. def form_post():
  89. text = []
  90. db = redis.Redis('localhost')
  91. for key in request.form:
  92. db.set(key, request.form[key])
  93. return render_template('display_results.html')