Welcome to the first lesson of our course on developing a personal tutor web application with Flask and DeepSeek! In this lesson, we will focus on setting up a basic tutor interface using Flask and HTML. This is an essential step in creating a user-friendly web application that enhances the learning experience. A well-designed interface is crucial for engaging students and ensuring they can interact with the tutor seamlessly.
To help you better visualize the interface we're building, here's a simple mockup of how the tutor web page will look after following the steps in this lesson:
- The header is at the top, welcoming the user.
- The messages area is in the center, where the conversation between the student and tutor appears.
- At the bottom, there is an input field for typing questions, a Send button, and a New Session button.
Flask is a lightweight web framework that makes it easy to build web applications in Python. One of its strengths is its built-in support for rendering HTML templates using Jinja2. Jinja2 is a modern and designer-friendly templating language that allows you to:
- Insert dynamic variables into HTML
- Use control structures like loops and conditionals
- Extend and include templates for reusable components
- Apply filters to modify variables during rendering
With Flask, you can easily serve HTML pages that include dynamic content, making it a great choice for building interactive web applications like a personal tutor.
To serve HTML templates with Flask, we first need to create a dedicated templates
directory in our project. This directory will store all our HTML template files that will be rendered by the application.
The templates
directory is placed at the root level of our project, making it easily accessible from our main application file. This organization follows the conventional structure for web applications and keeps our template files separate from the application logic.
Flask automatically looks for HTML templates in the templates
directory. To render a template, you use the render_template
function provided by Flask. This function takes the name of the template file and, optionally, any variables you want to pass to the template as keyword arguments.
Flask uses Jinja2 as its templating engine behind the scenes. When you call render_template
, Flask processes the template using Jinja2, which allows for dynamic content insertion and template logic. Later, we’ll pass dynamic values like the student’s name or previous messages into the template. For example, you could pass a username like this: return render_template("tutor.html", user_name="Student")
. Inside tutor.html
, you could then display this value using Jinja2 syntax like {{ user_name }}
.
Here is the complete code for a minimal Flask application that serves the tutor interface:
In this code:
- We import
Flask
,render_template
, andsession
from Flask. - We create a Flask application instance and set a secret key for session management.
- We initialize a
TutorController
instance to handle tutor-related logic. - We define a route for the root URL (
/
) using the@app.route("/")
decorator, specifying it accepts GET requests. - The
index
function ensures a user session exists and renders thetutor.html
template usingrender_template
. For now, we don't pass any variables, but we will later. - The
if __name__ == "__main__":
block runs the application in debug mode for development. - We specify the host as "0.0.0.0" to make the server publicly available and set the port to 3000.
- Setting
debug=True
enables automatic reloading when code changes and provides helpful error messages.
This approach allows us to serve dynamic content while maintaining a clean separation between our application logic and presentation layer.
The HTML template for the tutor interface begins with the basic structure of an HTML document. This includes the <!DOCTYPE html>
declaration, which defines the document type and version of HTML being used. The <html>
tag wraps the entire content of the page, and within it, the <head>
section is defined.
In the <head>
section, we set the title of the page to "Your Personal Tutor", establishing the foundation for the tutor interface.
Moving into the <body>
of the document, we start with a header section that sets the tone for the tutor interface. This section is designed to welcome students and encourage them to engage with the personal tutor.
The header includes a main heading (<h1>
) and a paragraph (<p>
), providing a friendly introduction to the tutoring service. This sets the stage for the interactive elements that follow.
Following the header, we define the tutor container, which is the core of the user interface. This section is responsible for displaying the conversation and providing input elements for student interaction.
The #messages
div is where the conversation between the student and tutor will appear, while the input field and buttons allow students to type and send questions. The "Send"
button triggers the sendQuery
function, and the "New Session"
button clears the conversation history, preparing the interface for a new tutoring session.
After setting up the HTML structure, we move on to adding interactivity to our tutor interface using JavaScript. This is done by placing a script section at the bottom of the HTML document, where we'll define the necessary JavaScript functions.
In this section, we create a script block within our HTML code to define JavaScript functions that enable interactivity in the tutor interface. By using plain JavaScript, we can directly manipulate HTML elements and handle user events. Placing the script at the end of the document ensures that all HTML elements are fully loaded before the script runs, preventing errors that might occur if the script tries to access elements that haven't been rendered yet. This approach allows us to seamlessly integrate JavaScript into our HTML, enhancing the functionality of our web application.
Before implementing the functions that handle tutor interactions, it's important to obtain references to the necessary DOM elements. This allows us to manipulate these elements directly within our JavaScript code.
By retrieving references to the messagesContainer
and messageInput
elements, we can easily update the tutor interface and handle student input. The messagesContainer
is where the conversation will be displayed, and the messageInput
is the field where students type their questions. These references are crucial for implementing the interactive functions that follow.
With the necessary DOM elements initialized, we can proceed to create functions that enhance the interactivity of our tutor interface. The startNewSession
function is designed to clear the conversation history, allowing students to begin a fresh tutoring session. This function is triggered when the "New Session" button is clicked.
The startNewSession
function clears all messages from the tutor interface's display, providing a clean slate for students to start a new learning session. This functionality is essential for resetting the conversation visually and enhancing the user experience by allowing multiple interactions without refreshing the page. It's important to note that this function, for now, only handles the visual clearing of the chat area. The corresponding backend logic to clear the session state will be implemented in a later lesson.
Additionally, by adding an event listener for the DOMContentLoaded
event, we ensure that the startNewSession
function is automatically called when the page finishes loading. This means the tutor interface is always initialized with a clean state, ready for student interaction as soon as the page is accessed. This approach enhances the learning experience by ensuring the tutor is ready to use immediately upon loading.
To effectively display the conversation in our tutor interface, we use the appendMessage
function. This function creates a new message element, assigns it a CSS class based on the message's origin (user or assistant), appends it to the conversation container, and ensures the view scrolls to the latest message.
The appendMessage
function is crucial for dynamically adding messages to the tutor interface. It creates a new <div>
element for each message, assigns a class to differentiate between student and tutor messages, and appends it to the messagesContainer
. This function also ensures that the conversation view automatically scrolls to the bottom, keeping the latest messages in view.
Building on the appendMessage
function, the sendQuery
function handles student input and updates the tutor interface. It processes the student's question, displays it, and simulates a response from the tutor. This function is triggered when the "Send" button is clicked or when the student presses Enter without holding Shift.
The sendQuery
function is responsible for capturing the student's input, ensuring it's not empty, and then displaying it in the tutor interface using the appendMessage
function. After sending the question, it clears the input field to prepare for the next question. It also simulates a response from the tutor by echoing the student's question back after a short delay, demonstrating basic interactivity in the tutoring application.
To enhance user experience, we can allow students to send questions by pressing the Enter key. This functionality is implemented by listening for the Enter key press event on the input field.
This code snippet listens for the keypress
event on the messageInput
field. When the Enter key is pressed without the Shift key, it prevents the default behavior (which would be to insert a newline) and calls the sendQuery
function. This allows students to quickly send questions using the keyboard, improving the tutor interface's usability.
In this lesson, we covered the essential steps for setting up a basic tutor interface using Flask and HTML. We explored how Flask serves HTML templates using its built-in Jinja2 support and how JavaScript is used to handle student interactions. By understanding the integration between Flask, HTML, and JavaScript, you have laid the groundwork for building a dynamic web application for personalized tutoring. As you move on to the practice exercises, focus on reinforcing these concepts and experimenting with the code to deepen your understanding. This foundational knowledge will be crucial as we continue to enhance the personal tutor's capabilities in future lessons.
