Python - 301 - Flask
  • 00 - Appealing API's
  • 01- A Smooth Start
  • 02 - Chief Configuration
  • 03 - Example Endpoint and an Adornable App
  • 04 - Modeling the Models
  • 05 - Scheming Schemas
  • 06 - Making Migrations
  • 07 - Uniformed Users
  • 08 - Alarming Authentication
  • 09 - Account Actions
  • 10 - Postman Prevalence and Examining Endpoints
  • 11 - Blog Post Blogging
Powered by GitBook
On this page

Was this helpful?

03 - Example Endpoint and an Adornable App

In this module we will build a sample endpoint to get oriented with RESTful services and see what an API can do in action

Previous02 - Chief ConfigurationNext04 - Modeling the Models

Last updated 6 years ago

Was this helpful?

Add the following code to your src/app.py . Then we will explain what the code is doing

This is everything we need in Flask to create a minimal application. This 24 line file of glory is going to be our workhorse for the majority of our app. This is where we will register views, blueprints (more on these later), and everything else the app will need. Think of this approach to Flask as a pizza. When we want to add extra functionality to our app, we can throw it in here and we won't have to do any major refactors.

In this file we also created an endpoint. This one won't be staying around for long. It is just a proof of concept. Note the @app.route syntax at the top of the page. This is called a decorator, and is how we tell Flask we are creating a URL. We also define the URL for the route (the first argument), and we define the HTTP methods allowed on the route. In this case, we will use this route for GET requests

Now we need to create the script that will run our application. Add the following code to run.py

We import the create_app function we wrote in app.py . Then we use our guard to make sure our code is only executed as a script. Then we grab our FLASK_ENV environment variable using the os module. Afterwards we get an app object from create_app using the environment variable we got on the last line and call the run() function. We can now run this script from our terminal and run our app.

Use python run.py in your terminal and open your browser to localhost:5000