import subprocess, os import requests from flask import Flask, jsonify, render_template, request, g FILES_DIR = "/media/usb" OCTOPRINT_PORT = '8080' app = Flask(__name__) ################################################ @app.route('/') def index(): return render_template('index.html') @app.route('/cleanbed') def cleanbed(): return render_template('cleanbed.html') @app.route('/applyglue') def applyglue(): return render_template('applyglue.html') @app.route('/choosefile') def choosefile(): g.filesDir = FILES_DIR return render_template('choosefile.html') @app.route('/printinfo') def printinfo(): filename = request.args.get('filename') return render_template('printinfo.html', filename=filename) @app.route('/printprogress') def printprogress(): filename = request.args.get('filename') return render_template('printprogress.html', filename=filename) @app.route('/cooling') def cooling(): return render_template('cooling.html') @app.route('/removeprint') def removeprint(): return render_template('removeprint.html') ################################################# @app.route('/octoUpload') def octoUpload(): hostname = request.args.get('hostname') filepath = request.args.get('filepath') filename = request.args.get('filename') url = 'http://' + hostname + ':' + OCTOPRINT_PORT + '/api/files/local' if(os.path.exists(filepath)): files = {'file': (filename, open(filepath, 'rb'))} headers = { 'X-Api-Key' : '19A7C56E31B74257955E49E5561D019D' } r = requests.post(url, files=files, headers=headers) r.encoding = 'utf-8' if(r.json['done']): return 'success' return 'fail' def control(): return render_template('control.html') @app.route('/octolist') def octolist(): return render_template('octolist.html') @app.route('/autolevel') def autolevel(): return render_template('autolevel.html') @app.route('/getDirectoryContents', methods=['POST']) def getDirectoryContents(): directory = request.form['dir'] if(os.path.exists(directory)): retval = '' files = os.listdir( directory ) retval += '
\n' return retval @app.route('/listfiles') def listFiles(): g.filesDir = FILES_DIR return render_template('list_files.html') @app.route('/showFile') def showFile(): filename = request.args.get('file','') f = open(filename, 'r') try: contents = f.read() finally: f.close() return contents # Testing @app.route('/test') def yesno(): return render_template('test.html') @app.route('/test/printyes') def printyes(): print("yes") return "" @app.route('/test/printno') def printno(): print("no") return "" if __name__ == '__main__': app.run(host='0.0.0.0',port=5001,debug=True)