-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
95 lines (85 loc) · 4.21 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from fastapi import FastAPI, Form, Response
from fastapi.responses import HTMLResponse, FileResponse
from pydantic import BaseModel
from typing import Union
import os
app = FastAPI()
class ScriptRequest(BaseModel):
script_type: str
content: str
@app.get("/", response_class=HTMLResponse)
def read_root():
html_content = """
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Script Generator</title>
<!-- Tailwind CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100 flex items-center justify-center min-h-screen">
<div class="bg-white p-8 rounded-lg shadow-lg w-full max-w-lg">
<h1 class="text-2xl font-bold text-center mb-6">Script Generator</h1>
<form action="/generate_script" method="post">
<div class="mb-4">
<label for="script_type" class="block text-gray-700 font-medium mb-2">Select Script Type:</label>
<select id="script_type" name="script_type" class="block w-full mt-1 p-2 border border-gray-300 rounded-md">
<option value="python">Python</option>
<option value="bash">Bash</option>
</select>
</div>
<div class="mb-4">
<label for="script_content" class="block text-gray-700 font-medium mb-2">Script Content:</label>
<textarea id="script_content" name="script_content" rows="10" class="block w-full mt-1 p-2 border border-gray-300 rounded-md"></textarea>
</div>
<div class="text-center">
<input type="submit" value="Generate Script" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded cursor-pointer">
</div>
</form>
</div>
</body>
</html>
"""
return html_content
@app.post("/generate_script", response_class=HTMLResponse)
def generate_script(script_type: str = Form(...), script_content: str = Form(...)):
if script_type == "python":
filename = "generated_script.py"
script = f"# Python Script\n{script_content}"
elif script_type == "bash":
filename = "generated_script.sh"
script = f"# Bash Script\n{script_content}"
else:
return "Unknown script type selected."
# Save the script to a file
with open(filename, "w") as file:
file.write(script)
return f"""
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Generated Script</title>
<!-- Tailwind CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100 flex items-center justify-center min-h-screen">
<div class="bg-white p-8 rounded-lg shadow-lg w-full max-w-lg">
<h2 class="text-xl font-bold text-center mb-6">Generated {script_type.capitalize()} Script:</h2>
<pre class="bg-gray-900 text-white p-4 rounded-lg overflow-x-auto">{script}</pre>
<div class="text-center mt-6">
<a href="/download_script/{filename}" class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded cursor-pointer">Download Script</a>
<a href="/" class="bg-gray-500 hover:bg-gray-700 text-white font-bold py-2 px-4 rounded ml-2 cursor-pointer">Go Back</a>
</div>
</div>
</body>
</html>
"""
@app.get("/download_script/{filename}", response_class=FileResponse)
def download_script(filename: str):
file_path = os.path.join(os.getcwd(), filename)
return FileResponse(path=file_path, filename=filename, media_type='application/octet-stream')
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=5353)