-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
152 lines (114 loc) · 5.11 KB
/
server.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from flask import Flask, request, render_template, redirect, url_for, jsonify, make_response
from flask_migrate import Migrate, migrate
from models import db, Mermaids
import os
import re
import helper_functions
from MermaidPY import code_to_mermaid
app = Flask(__name__)
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///datastore.db"
db.init_app(app)
migrate = Migrate(app, db)
from dotenv import load_dotenv
load_dotenv()
global SCRIPT_ROOT
SCRIPT_ROOT = os.path.dirname(os.path.abspath(__file__))
mermaid_directory = os.path.join(SCRIPT_ROOT, 'Mermaid_Files')
if not os.path.exists(mermaid_directory):
os.makedirs(mermaid_directory)
@app.route('/', methods = ['GET', 'POST'])
def home():
if request.method == 'GET':
mermaids = Mermaids.query.all()
return render_template('home.html', mermaids = mermaids)
mermaid_name = request.form.get('mermaid_name')
mermaid_description = request.form.get('mermaid_description')
mermaid = Mermaids(name = mermaid_name, description = mermaid_description)
db.session.add(mermaid)
db.session.commit()
mermaid.folder_path = helper_functions.create_mermaid_folder(mermaid.unique_id)
db.session.commit()
return redirect(url_for('show_mermaid', mermaid_id = mermaid.unique_id))
@app.route('/mermaid/<mermaid_id>')
def show_mermaid(mermaid_id):
mermaid = Mermaids.query.get(mermaid_id)
response = make_response(render_template('show_mermaid.html', mermaid = mermaid))
response.set_cookie('mermaid_unique_id', mermaid_id)
return response
@app.route('/delete_mermaid', methods = ['DELETE'])
def delete_mermaid():
unique_id = request.form.get('unique_id')
mermaid = Mermaids.query.get(unique_id)
helper_functions.delete_mermaid_folder(mermaid_unique_id = unique_id)
db.session.delete(mermaid)
db.session.commit()
return jsonify(delete_done = True, redirect_location = url_for('home'))
@app.route('/mermaid/<mermaid_id>/mermaid_code', methods = ['GET'])
def show_mermaidcode(mermaid_id):
response = make_response(render_template('show_mermaid_code.html'))
response.set_cookie('mermaid_unique_id', mermaid_id)
return response
@app.route('/mermaid/<mermaid_id>/mermaid_render', methods = ['GET'])
def show_mermaidrender(mermaid_id):
mermaid = Mermaids.query.get(mermaid_id)
with open(os.path.join(mermaid.folder_path, 'mermaid_code.txt'), 'r') as file_handle:
file_contents = file_handle.readlines()
response = make_response(render_template('show_mermaid_render.html', mermaid_code = ''.join(file_contents)))
response.set_cookie('mermaid_unique_id', mermaid_id)
return response
@app.route('/mermaid/<mermaid_id>/view_svg', methods = ['GET'])
def view_svg(mermaid_id):
mermaid = Mermaids.query.get(mermaid_id)
svg_path = os.path.join(mermaid.folder_path, 'svg_render.svg')
svg_exists = str(os.path.exists(svg_path))
response = make_response(render_template('svg_preview.html'))
response.set_cookie('mermaid_unique_id', mermaid_id)
response.set_cookie('svg_exists', svg_exists)
return response
@app.route('/get_file', methods = ['POST'])
def get_file():
unique_id = request.form.get('unique_id')
mermaid = Mermaids.query.get(unique_id)
required_file = request.form.get('file')
try:
file_path = os.path.join(mermaid.folder_path, required_file)
with open(file_path, 'r') as file_handle:
file_contents = file_handle.readlines()
return jsonify(result = True, file_contents = file_contents)
except Exception as exp:
print(exp)
return jsonify(result = False)
@app.route('/save_file', methods = ['POST'])
def save_file():
unique_id = request.form.get('unique_id')
mermaid = Mermaids.query.get(unique_id)
required_file = request.form.get('file')
try:
file_path = os.path.join(mermaid.folder_path, required_file)
with open(file_path, 'w') as file_handle:
file_contents = request.form.get('file_contents')
# file_contents = re.sub(r'\t{3}', '\t', file_contents)
file_handle.writelines(file_contents)
return jsonify(result = True)
except Exception as exp:
print(exp)
return jsonify(result = False)
@app.route('/process_codefile', methods = ['POST'])
def process_codefile():
unique_id = request.form.get('unique_id')
mermaid = Mermaids.query.get(unique_id)
try:
file_path = os.path.join(mermaid.folder_path, 'codefile.txt')
with open(file_path, 'r') as file_handle:
file_contents = file_handle.readlines()
flowchart = code_to_mermaid.perform_shunting_yard(text_contents = file_contents)
mermaid_code = code_to_mermaid.flowchart_to_mermaid(flowchart = flowchart)
with open(os.path.join(mermaid.folder_path, 'mermaid_code.txt'), 'w') as file_handle:
file_handle.writelines(mermaid_code)
return jsonify(result = True)
except Exception as exp:
print(exp)
return jsonify(result = False)
if __name__ == '__main__':
app.run(host = "0.0.0.0", port = "5537", debug = True)