Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Finalizing the Web Application

00:00 The Final Web App. The web app you’re creating uses only one URL endpoint, and because of this, you can’t rely on Flask to type check the user input via URL path component capturing as you did earlier on.

00:16 As a result of this, a number of changes will be made to the code. escape() will no longer be needed as the input validation will be part of that fahrenheit_from() function, so it will no longer be imported and will be removed from the line where the celsius key is read from the global request object. index will have a conditional statement added to check the value of celsius.

00:38 If it has a value, then you will call fahrenheit_from() to calculate the corresponding Fahrenheit degrees. If it’s an empty string, then you assign an empty string to the fahrenheit variable instead. fahrenheit_from() will no longer be associated with the URL endpoint, so the decorator will be removed. fahrenheit_from() will also have the tryexcept block reintroduced to allow for type checking.

01:04 On-screen, you’ll see these changes being made. You’re not using flask.escape() anymore, so you can remove it from the import statement.

01:22 As before, you’re fetching the user-submitted value through Flask’s global request object. By using the dictionary method .get(), you assure that an empty string gets returned if the key isn’t found. That’ll be the case if the page is loaded initially and the user hasn’t submitted the form yet.

01:41 If celsius has a value, then the form has had a value submitted to it, and fahrenheit will be calculated using the fahrenheit_from() function later in the script.

01:52 If celsius doesn’t have a value, then this is probably the first time the page has been loaded, so fahrenheit will be an empty string that is displayed when the page loads.

02:03 Some extra text is added to the form to make the page easier to use, and the value of fahrenheit is concatenated to the end of the return string.

02:26 fahrenheit_from() will no longer be accessible via a URL endpoint, so the decorator is removed. The tryexcept block is reinstated so that if a non-numeric value is entered, the function will now return an appropriate error message as text to be displayed to the user.

02:51 Your page will render correctly even though the way you’re adding these strings doesn’t represent valid HTML. This works thanks to the power of modern browsers.

03:02 Keep in mind that if you’re interested in diving deeper into web development, then you’ll need to learn HTML, which you can do as part of this Real Python learning path.

03:11 But for the sake of getting your Python script deployed online, this will do just fine. You should now be able to use your temperature conversion script inside your browser.

03:23 You can supply a Celsius temperature through the input box, click the button, and see the converted Fahrenheit result appear on the same web page. Since you’re using the default HTTP GET request, you can also see the submitted data appear in the URL.

03:39 In fact, you can even circumvent the form and provide your own value for celsius by supplying an appropriate address, similar to how you were able to use the conversion when you built the script without the HTML form.

03:51 Try typing the URL seen on-screen into your browser, and you’ll see the resulting temperature conversion appear on your page.

04:04 Now deploy the finished application again to Google App Engine using the gcloud app deploy command, as seen on-screen.

04:22 Once the deployment is done, go to the provided URL or run gcloud app browse to see your Python web application live on the Internet.

04:33 Test it out by adding different types of input. Once you’re satisfied, you can share your link with the world. At this point, you’ve successfully converted your Python script into a Python web app and deployed it to Google App Engine for online hosting.

04:50 You can use the same process to convert more of your Python scripts into web apps. Hosting your code online can make it accessible to more people over the Internet.

05:00 Go ahead and convert your favorite scripts into Flask applications and show them to the world. In the next section, we’ll take a look at what you’ve learned during this course.

Become a Member to join the conversation.