Untitled

 avatar
unknown
plain_text
5 months ago
1.5 kB
1
Indexable
import os
import subprocess
from flask import Flask, render_template_string, request

app = Flask(__name__)

# Directory where your Python scripts are located
SCRIPTS_DIR = './scripts'

# HTML template for listing scripts
template = '''
<!DOCTYPE html>
<html>
<head>
    <title>Python Script Launcher</title>
</head>
<body>
    <h1>Available Python Scripts</h1>
    <ul>
    {% for script in scripts %}
        <li>
            <form method="post" action="/run">
                <input type="hidden" name="script" value="{{ script }}">
                <button type="submit">{{ script }}</button>
            </form>
        </li>
    {% endfor %}
    </ul>
</body>
</html>
'''

@app.route('/')
def list_scripts():
    # Get all Python scripts in the directory
    scripts = [f for f in os.listdir(SCRIPTS_DIR) if f.endswith('.py')]
    return render_template_string(template, scripts=scripts)

@app.route('/run', methods=['POST'])
def run_script():
    script_name = request.form['script']
    script_path = os.path.join(SCRIPTS_DIR, script_name)
    
    # Run the selected script
    try:
        result = subprocess.run(['python', script_path], capture_output=True, text=True)
        output = result.stdout or result.stderr
        return f'<pre>{output}</pre><br><a href="/">Back to scripts</a>'
    except Exception as e:
        return f'<pre>Error: {e}</pre><br><a href="/">Back to scripts</a>'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)
Editor is loading...
Leave a Comment