Monday, February 1, 2021

Python Web Applications: Deploy Your Script as a Flask App

You wrote a Python script that you’re proud of, and now you want to show it off to the world. But how? Most people won’t know what to do with your .py file. Converting your script into a Python web application is a great solution to make your code usable for a broad audience.

In this tutorial, you’ll learn how to go from a local Python script to a fully deployed Flask web application that you can share with the world.

By the end of this tutorial, you’ll know:

  • What web applications are and how you can host them online
  • How to convert a Python script into a Flask web application
  • How to improve user experience by adding HTML to your Python code
  • How to deploy your Python web application to Google App Engine

In addition to walking through an example project, you’ll find a number of exercises throughout the tutorial. They’ll give you a chance to solidify what you’re learning through extra practice. You can also download the source code that you’ll use to build your web application by clicking the link below:

Brush Up on the Basics

In this section, you’ll get a theoretical footing in the different topics that you’ll work with during the practical part of this tutorial:

  • What types of Python code distribution exist
  • Why building a web application can be a good choice
  • What a web application is
  • How content gets delivered over the Internet
  • What web hosting means
  • Which hosting providers exist and which one to use

Brushing up on these topics can help you feel more confident when writing Python code for the Web. However, if you’re already familiar with them, then feel free to skip ahead, install the Google Cloud SDK, and start building your Python web app.

Distribute Your Python Code

Bringing your code to your users is called distribution. Traditionally, there are three different approaches you can use to distribute your code so that others can work with your programs:

  1. Python library
  2. Standalone program
  3. Python web application

You’ll take a closer look at each of these approaches below.

Python Library

If you’ve worked with Python’s extensive package ecosystem, then you’ve likely installed Python packages with pip. As a programmer, you might want to publish your Python package on PyPI to allow other users to access and use your code by installing it using pip:

$ python3 -m pip install <your-package-name>

After you’ve successfully published your code to PyPI, this command will install your package, including its dependencies, on any of your users’ computers, provided that they have an Internet connection.

If you don’t want to publish your code as a PyPI package, then you can still use Python’s built-in sdist command to create a source distribution or a Python wheel to create a built distribution to share with your users.

Distributing your code like this keeps it close to the original script you wrote and adds only what’s necessary for others to run it. However, using this approach also means that your users will need to run your code with Python. Many people who want to use your script’s functionality won’t have Python installed or won’t be familiar with the processes required to work directly with your code.

A more user-friendly way to present your code to potential users is to build a standalone program.

Standalone Program

Computer programs come in different shapes and forms, and there are multiple options for transforming your Python scripts into standalone programs. Below you’ll read about two possibilities:

  1. Packaging your code
  2. Building a GUI

Programs such as PyInstaller, py2app, py2exe, or Briefcase can help with packaging your code. They turn Python scripts into executable programs that can be used on different platforms without requiring your users to explicitly run the Python interpreter.

Read the full article at https://realpython.com/python-web-applications/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Real Python
read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...