Upload files to "/"
This commit is contained in:
parent
0637d34623
commit
c0725f614a
|
|
@ -0,0 +1,163 @@
|
|||
from flask import Flask, render_template_string, request
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# Define the HTML template for the list
|
||||
HTML_TEMPLATE = '''
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Chris/Alice Master/Kitten Contract (Read Only)</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
background-color: #000;
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
color: white;
|
||||
}
|
||||
h1 {
|
||||
color: #ff6347;
|
||||
}
|
||||
ul {
|
||||
list-style-type: decimal;
|
||||
padding-left: 20px;
|
||||
}
|
||||
li {
|
||||
background-color: #333;
|
||||
border-radius: 5px;
|
||||
margin-bottom: 10px;
|
||||
padding: 10px;
|
||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<center><h1>Master Chris</h1>
|
||||
<h1>Kitten Alice</h1>
|
||||
<h1>Contract</h1></center>
|
||||
<ul id="list">
|
||||
{% for item in items %}
|
||||
<li>{{ item }} <button onclick="deleteItem({{ loop.index0 }})">Delete</button></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
||||
<form id="addItemForm" onsubmit="addItem(event)">
|
||||
<input type="text" id="newItem" placeholder="Add new item">
|
||||
<button type="submit">Add Item</button>
|
||||
</form>
|
||||
|
||||
<script>
|
||||
async function addItem(event) {
|
||||
event.preventDefault();
|
||||
const newItem = document.getElementById('newItem').value;
|
||||
await fetch('/add', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ item: newItem }) });
|
||||
location.reload();
|
||||
}
|
||||
|
||||
async function deleteItem(index) {
|
||||
await fetch(`/delete/${index}`, { method: 'DELETE' });
|
||||
location.reload();
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
'''
|
||||
|
||||
# Define the HTML template for the editable list
|
||||
READ_ONLY_HTML_TEMPLATE = '''
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Chris/Alice Master/Kitten Contract</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
background-color: #000;
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
color: white;
|
||||
}
|
||||
h1 {
|
||||
color: #ff6347;
|
||||
}
|
||||
ul {
|
||||
list-style-type: decimal;
|
||||
padding-left: 20px;
|
||||
}
|
||||
li {
|
||||
background-color: #333;
|
||||
border-radius: 5px;
|
||||
margin-bottom: 10px;
|
||||
padding: 10px;
|
||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<center><h1>Master Chris</h1>
|
||||
<h1>Kitten Alice</h1>
|
||||
<h1>Contract</h1></center>
|
||||
<ul id="list">
|
||||
{% for item in items %}
|
||||
<li>{{ item }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
||||
<script>
|
||||
async function addItem(event) {
|
||||
event.preventDefault();
|
||||
const newItem = document.getElementById('newItem').value;
|
||||
await fetch('/add', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ item: newItem }) });
|
||||
location.reload();
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
'''
|
||||
|
||||
# Function to read the list from a file
|
||||
def read_list():
|
||||
try:
|
||||
with open('list.txt', 'r') as file:
|
||||
return [line.strip() for line in file.readlines()]
|
||||
except FileNotFoundError:
|
||||
return []
|
||||
|
||||
# Function to write the list to a file
|
||||
def write_list(items):
|
||||
with open('list.txt', 'w') as file:
|
||||
for item in items:
|
||||
file.write(f"{item}\n")
|
||||
|
||||
@app.route('/')
|
||||
def manage():
|
||||
items = read_list()
|
||||
return render_template_string(READ_ONLY_HTML_TEMPLATE, items=items)
|
||||
|
||||
@app.route('/add', methods=['POST'])
|
||||
def add_item():
|
||||
data = request.get_json()
|
||||
new_item = data['item']
|
||||
items = read_list()
|
||||
items.append(new_item)
|
||||
write_list(items)
|
||||
return '', 204
|
||||
|
||||
@app.route('/delete/<int:index>', methods=['DELETE'])
|
||||
def delete_item(index):
|
||||
items = read_list()
|
||||
if index < len(items):
|
||||
del items[index]
|
||||
write_list(items)
|
||||
return '', 204
|
||||
|
||||
@app.route('/manage/')
|
||||
def manage_read_only():
|
||||
items = read_list()
|
||||
return render_template_string(HTML_TEMPLATE, items=items)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True, host='0.0.0.0', port=5000)
|
||||
Loading…
Reference in New Issue