views.py 4.1 KB

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