-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
43 lines (33 loc) · 1.29 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
from flask import Flask, render_template, request
import zipfile
import os
app = Flask(__name__)
def read_code_snippets():
code_snippets = {}
dtx_filename = 'data.dtx'
with zipfile.ZipFile(dtx_filename, 'r') as zip_file:
with zip_file.open('.ids') as ids_file:
for line in ids_file:
line = line.decode('utf-8').strip()
index, filename = line.split(': ')
code_snippets[index] = {'filename': filename, 'code': None}
for index, snippet in code_snippets.items():
with zipfile.ZipFile(dtx_filename, 'r') as zip_file:
with zip_file.open(snippet['filename']) as code_file:
code_snippets[index]['code'] = code_file.read().decode('utf-8')
return code_snippets
@app.route('/')
def index():
code_snippets = read_code_snippets()
return render_template('index.html', code_snippets=code_snippets)
@app.route('/code')
def get_code():
code_id = request.args.get('id')
code_snippets = read_code_snippets()
if code_id in code_snippets:
code = code_snippets[code_id]['code']
return render_template('code.html', code=code)
else:
return "Code snippet not found."
if __name__ == '__main__':
app.run(debug=True)