This repository has been archived by the owner on Oct 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathapp.py
66 lines (46 loc) · 1.92 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
from flask import Flask, render_template, request
import os
app = Flask(__name__)
# default page
@app.route('/')
def index():
return render_template('index.html')
# render home page
@app.route('/home')
def home():
""" Reder home page """
return render_template('index.html')
# render about page
@app.route('/about')
def about():
""" Render about page"""
return render_template('about.html')
@app.route('/language', methods=['POST'])
def language():
"""
Fetch question name and file path based on the selected language and returns the home page with the questions information
"""
language = request.form['language']
solutions_dir = f"{language}/"
questions = [] # list to store the information related to question
for filename in os.listdir(solutions_dir):
# question name would not contain underscore and extension
question_name = filename.replace('_', ' ').replace('.cpp', '').replace('.java', '').replace('.py', '')
questions.append({'name': question_name, 'file': filename})
return render_template('index.html', questions=questions, language=language)
@app.route('/problem/<filename>')
def problem_solution(filename):
"""
Read the solution code from the file and return the problem_solution.html page to show the solution code to user
"""
# get the language which is passed as a parameter from the index.html
language = request.args.get('language')
if not language:
return "Language not specified. Please select a language." # Error Message if no Language is selected
file_path = f"{language}/{filename}" # Filepath
with open(file_path, 'r') as f:
code = f.read()
question_name = filename.replace('_', ' ').replace('.cpp', '').replace('.java', '').replace('.py', '')
return render_template('problem_solution.html', code=code, question_name=question_name)
if __name__ == '__main__':
app.run(debug=True)