Spaces:
Sleeping
Sleeping
| from flask import Flask, render_template_string, request, redirect, url_for, session | |
| import os | |
| import platform | |
| app = Flask(__name__) | |
| app.secret_key = "mini_os_secret_key" | |
| # ---------------- LOGIN PAGE ---------------- # | |
| login_page = """ | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Mini OS Login</title> | |
| <style> | |
| body { background:black; color:white; text-align:center; font-family:Arial; } | |
| .box { margin-top:100px; } | |
| input { padding:10px; margin:5px; width:200px; } | |
| button { padding:10px 20px; background:green; color:white; border:none; cursor:pointer; } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="box"> | |
| <h1>Mini Python OS</h1> | |
| <form method="POST"> | |
| <input name="username" placeholder="Username" required><br> | |
| <input name="password" type="password" placeholder="Password" required><br> | |
| <button type="submit">Login</button> | |
| </form> | |
| <p>Default Login: admin / 1234</p> | |
| <p style="color:red;">{{error}}</p> | |
| </div> | |
| </body> | |
| </html> | |
| """ | |
| # ---------------- DASHBOARD ---------------- # | |
| dashboard_page = """ | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Mini OS Desktop</title> | |
| <style> | |
| body { background:#1e1e1e; color:white; font-family:Arial; text-align:center; } | |
| button { padding:15px; margin:10px; width:220px; cursor:pointer; } | |
| .container { margin-top:50px; } | |
| </style> | |
| </head> | |
| <body> | |
| <h1>Mini Python OS Desktop</h1> | |
| <div class="container"> | |
| <a href="/files"><button>File Explorer</button></a> | |
| <a href="/notepad"><button>Notepad</button></a> | |
| <a href="/calculator"><button>Calculator</button></a> | |
| <a href="/system"><button>System Info</button></a> | |
| <a href="https://google.com" target="_blank"><button>Open Google</button></a> | |
| <a href="https://chat.openai.com" target="_blank"><button>Open ChatGPT</button></a> | |
| <br><br> | |
| <a href="/logout"><button style="background:red;color:white;">Logout</button></a> | |
| </div> | |
| </body> | |
| </html> | |
| """ | |
| # ---------------- FILE EXPLORER ---------------- # | |
| files_page = """ | |
| <h2>File Explorer</h2> | |
| <ul> | |
| {% for f in files %} | |
| <li>{{f}}</li> | |
| {% endfor %} | |
| </ul> | |
| <br> | |
| <a href="/dashboard">Back</a> | |
| """ | |
| # ---------------- NOTEPAD ---------------- # | |
| notepad_page = """ | |
| <h2>Notepad</h2> | |
| <form method="POST"> | |
| <textarea name="text" rows="15" cols="60"></textarea><br><br> | |
| <button type="submit">Save</button> | |
| </form> | |
| <p style="color:lightgreen;">{{msg}}</p> | |
| <br> | |
| <a href="/dashboard">Back</a> | |
| """ | |
| # ---------------- CALCULATOR ---------------- # | |
| calculator_page = """ | |
| <h2>Calculator</h2> | |
| <form method="POST"> | |
| <input name="exp" placeholder="Enter expression" required> | |
| <button type="submit">Calculate</button> | |
| </form> | |
| <h3>Result: {{result}}</h3> | |
| <br> | |
| <a href="/dashboard">Back</a> | |
| """ | |
| # ---------------- SYSTEM INFO ---------------- # | |
| system_page = """ | |
| <h2>System Info</h2> | |
| <pre> | |
| System: {{system}} | |
| Node: {{node}} | |
| Release: {{release}} | |
| Version: {{version}} | |
| Machine: {{machine}} | |
| Processor: {{processor}} | |
| </pre> | |
| <br> | |
| <a href="/dashboard">Back</a> | |
| """ | |
| # ---------------- ROUTES ---------------- # | |
| def login(): | |
| error = "" | |
| if request.method == "POST": | |
| username = request.form.get("username") | |
| password = request.form.get("password") | |
| if username == "admin" and password == "1234": | |
| session["user"] = username | |
| return redirect(url_for("dashboard")) # FIXED REDIRECT | |
| else: | |
| error = "Invalid username or password" | |
| return render_template_string(login_page, error=error) | |
| def dashboard(): | |
| if "user" not in session: | |
| return redirect(url_for("login")) | |
| return render_template_string(dashboard_page) | |
| def files(): | |
| if "user" not in session: | |
| return redirect(url_for("login")) | |
| file_list = os.listdir(".") | |
| return render_template_string(files_page, files=file_list) | |
| def notepad(): | |
| if "user" not in session: | |
| return redirect(url_for("login")) | |
| msg = "" | |
| if request.method == "POST": | |
| text = request.form.get("text") | |
| with open("note.txt", "w", encoding="utf-8") as f: | |
| f.write(text) | |
| msg = "File saved as note.txt" | |
| return render_template_string(notepad_page, msg=msg) | |
| def calculator(): | |
| if "user" not in session: | |
| return redirect(url_for("login")) | |
| result = "" | |
| if request.method == "POST": | |
| try: | |
| exp = request.form.get("exp") | |
| result = eval(exp) | |
| except: | |
| result = "Error" | |
| return render_template_string(calculator_page, result=result) | |
| def system(): | |
| if "user" not in session: | |
| return redirect(url_for("login")) | |
| return render_template_string(system_page, | |
| system=platform.system(), | |
| node=platform.node(), | |
| release=platform.release(), | |
| version=platform.version(), | |
| machine=platform.machine(), | |
| processor=platform.processor() | |
| ) | |
| def logout(): | |
| session.clear() | |
| return redirect(url_for("login")) | |
| # ---------------- RUN APP ---------------- # | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860, debug=True) |