-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
77 lines (60 loc) · 2.29 KB
/
app.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
import hashlib
import sqlite3
from flask import Flask, render_template, request, redirect, url_for, jsonify, g
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from modules.db.bootstrap import create_schema
app = Flask(__name__)
# Setup the limiter
limiter = Limiter(
get_remote_address,
app=app,
default_limits=["5 per day"],
)
@limiter.exempt
@app.route('/', methods=['GET'])
def home():
return render_template('index.html')
@limiter.exempt
@app.route('/submit_code', methods=['POST'])
def submit_code():
code = request.form['code']
hash_obj = hashlib.sha256()
hash_obj.update(code.encode('utf-8'))
unique_id = hash_obj.hexdigest()
db = get_db()
db.execute("INSERT OR IGNORE INTO snippets(snippet_id, snippet) values (?,?)", (unique_id,code))
db.commit()
db.close()
return redirect(url_for('get_code', code_id=unique_id))
@app.route('/explain_code', methods=['POST'])
def explain_code():
unique_id = request.get_json()['id']
db = get_db()
x=db.execute("SELECT snippet from snippets where snippet_id=(?)", (unique_id,)).fetchone()
db.commit()
print(x[0])
# explained=explain_code_snippet(codes[unique_id])
# print(explained)
return jsonify({'content': str("""
- The code snippet simply prints the string "hello" to the console.
- The `print()` function is used to output text or data to the terminal or console.
- In this case, the `print()` function is called with the argument "hello", causing it to display the text "hello" when the code is executed.
- This code demonstrates a basic use of the `print()` function in Python to produce output.
- The `print()` function is commonly used for debugging, logging, or providing information to the user in Python programs.
""")})
@limiter.exempt
@app.route('/code/<code_id>', methods=['GET'])
def get_code(code_id):
db = get_db()
x=db.execute("SELECT snippet from snippets where snippet_id=(?)", (code_id,)).fetchone()
db.commit()
return render_template('code_display.html', code=x[0] if x is not None else "Not found", code_id=code_id)
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect("snippets.db")
return db
if __name__ == '__main__':
create_schema()
app.run()