The Next Step: Getting Started¶ In this tutorial, we’ll create a pipeline to create a new web application that uses a RESTful API.
If you’re using Django, you can skip this section and skip ahead to creating the Django web application in Python.
If not, see How to Create a Django Web Application.
If using Python, the next step will be to import the Flask framework, import the Python dependencies, and run the Flask application.
If the Flask app is running, the pipeline will create a Django application.
The first thing you’ll want to do is create the Django app and start it.
Create the Flask Application¶ The first step in building a web app is to create the Flask web application, which is a Django template application.
Flask is a Python framework that enables you to write a simple Python web application by building an interactive Python web page.
Flask also provides several built-in templates for you to use.
You can build a Flask application by importing the Flask package.
Flask templates have the name flask.py and are available as part of the Python distribution.
Flask allows you to define template files by using a pattern matching pattern, such as: #!/usr/bin/python import Flask from flask import FlaskApp from flask.template import Template from flaskapp import Application def create_template(template): template.content_type = ‘text/plain’ template.header = ‘Content-Type: application/x-www-form-urlencoded’ template = FlaskApp.
Templates.new(template) FlaskAppTemplate(template, header=template.header) The Flask app uses Flask templates to display its contents.
The template name is a template string, and it can contain one or more template files.
The Template object in the FlaskApp class is a subclass of Template that provides a few useful functions.
The default template, Template.template, will be used.
The following example creates a Flask template application by using Flask templates: from flask app import Flask, Template from FlaskApp import Application from flaskapps.templates import Template template = Template.new template.name = ‘My Flask application’ template_name = Template[ ‘name’ ] Template.add_templates(Application) FlaskAppsTemplates() The Application class inherits from the Flask class, which provides several useful functions that you can use to create web applications.
The Application subclass inherits Flask’s FlaskApp template class, a subclass that provides several functions.
For example, you may define a custom template that is automatically generated for you by Flask.
This template will contain information about the web page you are working on and the resources you need to create that page.
Here is an example of the Template.create_template function in FlaskApp: from application import Flask app = FlaskApps.
Templatetable() app.add(Template(name=’my_app’, template_content_types=’text/html’, header=’Content-type: application-style’, template=Template)) You can also use templates to define custom headers.
For instance, if you need a custom header for a message from your application, you could define this header:
The first line defines a template for the template, which defines a header for the message content type, and defines the header name.
The second line defines the content type and the title of the message.
The third line defines which header the message should use.
The fourth line adds a template to the application.
You will also need to pass a template in the Template object.
For more information about Template, see Template Objects.
To build a template application, import Flask in your terminal.
For most Python packages, it is a good idea to import all of the necessary dependencies.
For Flask, you’ll need to import Flask and FlaskApps to import them.
To add the Flask packages, first install them with pip install Flask: pip install flaskapp flaskapp.apps FlaskApps¶ For more details about how to use Flask, see Flask Apps.
In order to run the application, it will use the Flask templates.py file created by the app class.
The app class will be an instance of the FlaskApplication class.
It has the following fields: name: the name of the web application template: the template file to be used to present the web content title: the title for the web pages documentation: the Django documentation page for the application path: the directory where the application is stored application_dir: the application directory template_path: the root directory of the application template_file: the content directory template: a file that defines the template name.
For the example above, we will use Flask templates for the header and the message fields.
The name field defines the name for the website.
The title and message fields define the content for the page.
To run the app, call the run() method on the Flask template: >>> from application.app import Flask