Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
66 lines (56 loc) · 2.1 KB
/
Copy pathapp.py
File metadata and controls
66 lines (56 loc) · 2.1 KB
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
import os
import json
from flask import Flask, request, jsonify, send_from_directory
app = Flask(__name__, static_folder='static')
DATA_DIR = 'data'
PAGES_DIR = os.path.join(DATA_DIR, 'pages')
GRID_FILE = os.path.join(DATA_DIR, 'main.scape')
@app.route('/')
def index():
return app.send_static_file('index.html')
@app.route('/api/grid', methods=['GET'])
def get_grid():
if not os.path.exists(GRID_FILE) or os.path.getsize(GRID_FILE) == 0:
return jsonify({})
with open(GRID_FILE, 'r') as f:
content = f.read()
# In case the file is just whitespace
if not content.strip():
return jsonify({})
return app.response_class(content, mimetype='application/json')
@app.route('/api/grid', methods=['POST'])
def save_grid():
data = request.get_json()
with open(GRID_FILE, 'w') as f:
json.dump(data, f)
return jsonify({"status": "success"})
@app.route('/api/page/<page_name>', methods=['GET'])
def get_page(page_name):
page_path = os.path.join(PAGES_DIR, f"{page_name}.md")
if not os.path.exists(page_path):
return jsonify({"content": ""})
with open(page_path, 'r') as f:
content = f.read()
return jsonify({"content": content})
@app.route('/api/page/<page_name>', methods=['POST'])
def save_page(page_name):
page_path = os.path.join(PAGES_DIR, f"{page_name}.md")
os.makedirs(os.path.dirname(page_path), exist_ok=True)
data = request.get_json()
with open(page_path, 'w') as f:
f.write(data['content'])
return jsonify({"status": "success"})
@app.route('/api/pages', methods=['GET'])
def get_pages():
if not os.path.exists(PAGES_DIR):
return jsonify([])
pages = [f.replace('.md', '') for f in os.listdir(PAGES_DIR) if f.endswith('.md')]
return jsonify(pages)
@app.route('/<page_name>')
def show_page(page_name):
# This route serves the main index.html for any top-level path.
# The frontend JavaScript will then handle loading the correct page
# based on the URL path.
return app.send_static_file('index.html')
if __name__ == '__main__':
app.run(debug=True, port=5001)