Skip to content

Commit fc1345c

Browse files
committed
seprated and optimized theme selection, perfomance and code base improvement
1 parent b3ebede commit fc1345c

File tree

8 files changed

+31
-24
lines changed

8 files changed

+31
-24
lines changed

app.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from routes.browse_routes import browse_blueprint
44
from routes.login_routes import login_blueprint
55
from routes.view_routes import view_blueprint
6+
from routes.theme import theme_selection
67

78

89
app = Flask(__name__)
@@ -15,9 +16,9 @@
1516

1617
@app.errorhandler(404)
1718
def page_not_found(error):
18-
theme_preference = request.cookies.get('theme', 'light') # Default to 'light' if cookie not found
19-
theme_css = theme_preference + "_theme"
20-
return render_template('components/error.html',theme_css=theme_css), 404
19+
return render_template('components/error.html',theme_css=theme_selection()), 404
20+
21+
2122

2223
if __name__ == '__main__':
2324
app.run()

data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
# make this mongo db in future
22
Python = {
33
'courseName': 'Python', #used on search results & link route
44
'name': 'Python Programming',

routes/browse_routes.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
from lecturesData import lecturesData
44
from .details import course_detail
55
from .course_mapping import course_mapping
6+
from .theme import theme_selection
7+
8+
9+
610

711
browse_blueprint = Blueprint('browse', __name__)
812

@@ -11,8 +15,7 @@
1115
def browse():
1216
trending = menuData.trending
1317
allCourses = menuData.all
14-
theme_preference = request.cookies.get('theme', 'light') # Default to 'light' if cookie not found
15-
theme_css = theme_preference + "_theme"
18+
1619

1720
# If the form is submitted (POST request), get the search keyword from the form
1821
if request.method == 'POST':
@@ -22,10 +25,10 @@ def browse():
2225
for course_name, course_data in course_mapping.items():
2326
if search_keyword.lower() in course_name.lower():
2427
filtered_courses.append(course_data)
25-
return render_template('browse.html',theme_css=theme_css, trending=trending, allCourses=allCourses, search_keyword=search_keyword, search_results=filtered_courses)
28+
return render_template('browse.html',theme_css=theme_selection(), trending=trending, allCourses=allCourses, search_keyword=search_keyword, search_results=filtered_courses)
2629

2730
# If it's a GET request, show the regular browse page without search results
28-
return render_template('browse.html', trending=trending, allCourses=allCourses, theme_css=theme_css)
31+
return render_template('browse.html', trending=trending, allCourses=allCourses, theme_css=theme_selection())
2932

3033

3134

routes/details.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
import data, menuData
44
from lecturesData import lecturesData
55
from .course_mapping import course_mapping
6+
from .theme import theme_selection
7+
68

79
def course_detail(course_name):
8-
theme_preference = request.cookies.get('theme', 'light')
9-
theme_css = theme_preference + "_theme"
1010

1111
if course_name not in course_mapping:
12-
return render_template('components/error.html', theme_css=theme_css)
12+
return render_template('components/error.html', theme_css=theme_selection())
1313

1414
course = course_mapping[course_name]
1515
lectures_data = lecturesData.get(course_name, [])
1616
trending = menuData.trending
1717

18-
return render_template('details.html', course=course, lectures_data=lectures_data, trending=trending, theme_css=theme_css)
18+
return render_template('details.html', course=course, lectures_data=lectures_data, trending=trending, theme_css=theme_selection())

routes/index_routes.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from flask import Blueprint, render_template, request
22
import menuData,data
3-
3+
from .theme import theme_selection
44

55
index_blueprint = Blueprint('index', __name__)
66

77
@index_blueprint.route('/',endpoint='index')
88
def index():
99
trending = menuData.trending
10-
theme_preference = request.cookies.get('theme', 'light') # Default to 'light' if cookie not found
11-
theme_css = theme_preference + "_theme" # Assuming your CSS files are named "light_theme.css" and "dark_theme.css
10+
1211

13-
return render_template('index.html', trending=trending, theme_css=theme_css)
12+
return render_template('index.html', trending=trending, theme_css=theme_selection())

routes/login_routes.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
from flask import Blueprint, render_template, request
2-
2+
from .theme import theme_selection
33

44
login_blueprint = Blueprint('login', __name__)
55

66
@login_blueprint.route('/login')
77
def login():
8-
theme_preference = request.cookies.get('theme', 'light') # Default to 'light' if cookie not found
9-
theme_css = theme_preference + "_theme" # Assuming your CSS files are named "light_theme.css" and "dark_theme.css
108

11-
return render_template('login.html', theme_css=theme_css)
9+
return render_template('login.html', theme_css=theme_selection())

routes/theme.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from flask import request
2+
3+
4+
5+
def theme_selection():
6+
theme_preference = request.cookies.get('theme', 'light') # Default to 'light' if cookie not found
7+
theme_css = theme_preference + "_theme"
8+
return theme_css

routes/view_routes.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
from flask import Blueprint, render_template, request
2-
2+
from .theme import theme_selection
33

44
view_blueprint = Blueprint('view', __name__)
55

66
@view_blueprint.route('/view')
77
def views():
8-
theme_preference = request.cookies.get('theme', 'light') # Default to 'light' if cookie not found
9-
theme_css = theme_preference + "_theme" # Assuming your CSS files are named "light_theme.css" and "dark_theme.css
108

119
link = request.args.get('link')
1210
thumbnail = request.args.get('thumbnail')
1311
topic = request.args.get('topics')
1412
courseName = request.args.get('courseName')
1513
video_link = request.args.get('video_link')
1614

17-
return render_template('view.html', theme_css=theme_css, VIDEO_ID=link, thumbnail=thumbnail, topic=topic, courseName=courseName, video_link=video_link)
15+
return render_template('view.html', theme_css=theme_selection(), VIDEO_ID=link, thumbnail=thumbnail, topic=topic, courseName=courseName, video_link=video_link)

0 commit comments

Comments
 (0)