Converting a Script to a Web App
00:00 Convert a Script into a Web Application. Since this course is about creating and deploying Python web applications from code you already have, the Python code for the temperature converter script is provided for you here, both on-screen and in the course files.
00:49 This is a short script that allows a user to convert a Celsius temperature to the equivalent Fahrenheit temperature. With this working script in hand, you’ll now need to change the code to integrate it into your Flask app.
Flask separates different tasks into different functions that are each assigned a route through the
@app.route decorator. When the user visits the specified route via its URL, the code inside the corresponding function gets executed.
Currently, both of your functions are triggered by the same route. When a user visits that route, Flask picks the first function that matches it and executes that code. In your case, this means that
fahrenheit_from() never gets executed because
index() matches the same route and gets called first.
02:41 The second function will need its own unique route to be accessible. Additionally, you still need to allow users to provide input to the function. You can solve both these tasks in one by telling Flask to treat any remaining part of the URL following the base URL as a value and pass it on to your function.
The angle bracket syntax tells Flask to capture any text following the base URL and pass it on to the function the decorator wraps as the variable
celsius. Note that
celsius as an input.
03:29 Make sure the URL path component you’re capturing has the same name as the parameter you’re passing to your function. Otherwise, Flask will be confused and will let you know about it by presenting you with an error message.
03:42 Head back to your web browser and try out the new functionality using Flask’s development server. You’re now able to access both of your functions through your web app using different URL endpoints. If you go to the base URL, then you’ll see the short message from before.
04:10 Even the error handling from your script is still functional and displays a message when a user enters a non-numeric input. Your web app handles the same functionality as your Python script did locally, only now you can deploy it to the Internet.
04:25 Flask is a mature web framework that allows you to hand over a lot of tasks to its internals. For example, you can let Flask take care of type checking the input to your function and returning an error message if it doesn’t fit.
int: before the variable name tells Flask to check whether the input it receives from the URL can be converted to an integer. If it can, then the content is passed on to
fahrenheit_from(). If it can’t, then Flask displays a
Not Found error page.
Note that the
Not Found error message means that Flask attempted to match the path component it snipped off from the URL with any of the functions it knows about. However, the only patterns it currently knows about are the empty base path and the base path followed by a number, such as
42. Since text such as
/hello doesn’t match any of these patterns, it tells you that the requested URL was not found on the server.
06:27 After successfully deploying your temperature conversion web app to the Internet, you now have a link that you can share with other people and allow them to convert Celsius temperatures to Fahrenheit. However, the interface still looks quite basic and the web app functions more like an API than a front-end web app.
06:56 If you want to create more intuitive interfaces, then you’ll need to start using at least a little bit of HTML. In the next section, you’ll keep iterating over your code and use HTML to create an input box that allows users to enter a number directly on the page rather than through the URL.
Become a Member to join the conversation.