# Default Flask Template For Stand Alone Web UI Applications # K0NxT3D - 2024 # Notes: This version does not launch via torsocks. # Adding a launcher script will alleviate this issue. import os import subprocess import webbrowser from threading import Timer from threading import Thread from flask import Flask, render_template_string, request # In Default requirements.txt File As 2.0.3 TITLE = "Flask Template" VERSION = '1.0.0' APPNAME = f"{TITLE} {VERSION}" PORT = 26001 app = Flask(TITLE) @app.route('/', methods=['GET', 'POST']) def index(): return render_template_string(TEMPLATE, appname=APPNAME, title=TITLE, version=VERSION) # HTML Template With Bootstrap And Oswald Font Embedded - Custom CSS Files Can Be Called In . TEMPLATE = """ {{appname}}
{{title}} {{version}}

Page Title

""" # This Is "The Magic" Shutdown Logic. @app.route('/exit', methods=['GET']) def exit_app(): """Shutdown the Flask application gracefully, restricted to localhost.""" if request.remote_addr != '127.0.0.1': # Only Allow Localhost Access - Security return "Forbidden", 403 try: # HTML Content With JavaScript For Delay Before Redirect. html_content = """ {{title}} - {{version}}

Application Terminated

You May Now Close This Window.
""" # Render The HTML Page With The Redirect Logic response = render_template_string(html_content, appname=APPNAME, title=TITLE, version=VERSION) # Schedule The Server Shutdown After Javascript Redirect - This Can Be Adjusted For Slower Servers / Sites. Timer(1, os._exit, args=[0]).start() # Shutdown Server return response except Exception as e: return f"Error During Shutdown: {e}", 500 def open_browser(): """Open the default web browser to the Flask app's URL.""" webbrowser.open(f"http://127.0.0.1:{PORT}") if __name__ == "__main__": Thread(target=open_browser).start() app.run(debug=True, port=PORT, use_reloader=False)