Code Monkey home page Code Monkey logo

python-p4-flask-restful-cr-lab's Introduction

REST Create and Retrieve Lab

Learning Goals

  • Build RESTful APIs that are easy to navigate and use in applications.
  • Develop a Flask API with successful frontend connections via fetch().
  • Integrate create and retrieve routes with the associated actions to return the appropriate JSON data.

Key Vocab

  • Representational State Transfer (REST): a convention for developing applications that use HTTP in a consistent, human-readable, machine-readable way.
  • Application Programming Interface (API): a software application that allows two or more software applications to communicate with one another. Can be standalone or incorporated into a larger product.
  • HTTP Request Method: assets of HTTP requests that tell the server which actions the client is attempting to perform on the located resource.
  • GET: the most common HTTP request method. Signifies that the client is attempting to view the located resource.
  • POST: the second most common HTTP request method. Signifies that the client is attempting to submit a form to create a new resource.
  • PATCH: an HTTP request method that signifies that the client is attempting to update a resource with new information.
  • PUT: an HTTP request method that signifies that the client is attempting to update a resource with new information contained in a complete record.
  • DELETE: an HTTP request method that signifies that the client is attempting to delete a resource.

Introduction

In this lab, we'll be building an API for a plant store! In addition to our usual Flask code, there is code for a React frontend application in the client directory.

The code for the frontend application is done. Your job is to create the Flask API so that the fetch requests on the frontend work successfully.


Instructions

The React application is in the client directory. To set it up, from the root directory, run:

$ npm install --prefix client

Using --prefix client will run the npm command within the client directory.

To set up your backend, run:

$ pipenv install; pipenv shell

Then navigate to the server/ directory to run your Python code.

First, you will need to set up your database. Go ahead and run the following command to create the instance/app.db database file:

$ flask db upgrade head

To see how the React application and Flask API are interacting, first, you will need to set the default port number to match the proxy setup in the client's package.json. In this case, the port number is 5555.

export FLASK_RUN_PORT=5555

Now you can run the Flask application in one terminal by running:

$ flask run

Then, open another terminal and run React:

$ npm start --prefix client

Each application will run on its own port on localhost:

Take a look through the components in the client/src/components/ folder to get a feel for what our app does. Note that the fetch requests in the frontend (in NewPlantForm and PlantPage) don't include the backend domain:

fetch("/plants");
// instead of fetch("http://localhost:5000/plants")

This is because we are proxying these requests to our Flask API.


Deliverables

Model

Edit the Plant model in models.py to match this specification:

Column Name Data Type
name string
image string
price decimal

After defining the columns for the Plant model and saving the file, do the following to update and seed the plant table:

  1. Create a revision that tracks your changes to models.py
$ flask db revision --autogenerate -m'add columns to table'
  1. Upgrade the db to the latest revision
$ flask db upgrade head
  1. Seed the database
$ python seed.py

Routes

Your API should have the following routes as well as the associated controller actions that return the appropriate JSON data:

Index Route

GET /plants


Response Body
-------
[
  {
    "id": 1,
    "name": "Aloe",
    "image": "./images/aloe.jpg",
    "price": 11.50
  },
  {
    "id": 2,
    "name": "ZZ Plant",
    "image": "./images/zz-plant.jpg",
    "price": 25.98
  }
]

Show Route

GET /plants/:id


Response Body
------
{
  "id": 1,
  "name": "Aloe",
  "image": "./images/aloe.jpg",
  "price": 11.50
}

Create Route

POST /plants


Headers
-------
Content-Type: application/json


Request Body
------
{
  "name": "Aloe",
  "image": "./images/aloe.jpg",
  "price": 11.50
}


Response Body
-------
{
  "id": 1,
  "name": "Aloe",
  "image": "./images/aloe.jpg",
  "price": 11.50
}

Note 1: When adding image URLs, you will need to use absolute URLs from the internet; we have only uploaded the two images to this project directory.

Note 2: Due to the structure of the client, you will need to use the get_json() method to retrieve data for the create route. When you write your own clients, you can decide whether data is passed to the backend via forms or raw JSON.

Once all the tests are passing, start up the React app and explore the functionality to see how the routes you created are being used.

Resources

python-p4-flask-restful-cr-lab's People

Contributors

gnappo1 avatar jlboba avatar linda-seiter avatar lizbur10 avatar mishelbyt avatar pgill97 avatar professor-ben avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-p4-flask-restful-cr-lab's Issues

npm start failing

Canvas Link

https://learning.flatironschool.com/courses/6259/assignments/233475?module_item_id=547406

Concern

Running npm start --prefix client returned '...bad option: --openssl-legacy-provider'.

Looking online I found a solution to change

"scripts": {
    "build": "react-scripts --openssl-legacy-provider build",
    "start": "react-scripts --openssl-legacy-provider start"
  }

in clients/package.json to

scripts": {
  "build": "react-scripts build --openssl-legacy-provider",
  "start": "react-scripts start --openssl-legacy-provider"
}

Now npm start works for me.
Reference: coreui/coreui-free-react-admin-template#336

Additional Context

No response

Suggested Changes

No response

Told me to seed early

Canvas Link

https://learning.flatironschool.com/courses/6463/assignments/250527?module_item_id=591885

Concern

The instructions told me seed before creating the models which led to multiple errors a quite confusing python error trace stack.

It would be helpful if I was only told to seed after making the tables that I am supposed to be using for the seed data.

I enjoyed this lab but found it confusing at first due to the instructions. Thanks!

Additional Context

No response

Suggested Changes

No response

Had to re-run db init

Canvas Link

https://learning.flatironschool.com/courses/6182/assignments/209743?module_item_id=483942

Concern

When I tried running my migration for this lab using flask db revision --autogenerate, I kept getting an ImportError: can't find Python file migrations/env.py. The only thing within the migrations folder was the versions folder (which was empty), and the .gitignore. I tried running flask db init, but had to remove the migrations folder first in order to do so successfully. Once I did that, I was able to create and run my migration fine. Not sure if this is something to do with the lab setup, but it may be an issue students run into as well?

Additional Context

No response

Suggested Changes

No response

Missing sqlalchemy-serializer in Pipfile

Canvas Link

https://learning.flatironschool.com/courses/6182/assignments/209743?module_item_id=483942

Concern

I ran 'flask run' per the instructions in the readme.
The terminal printed this error

python-p4-flask-restful-cr-lab-main/server/models.py", line 2, in <module>
    from sqlalchemy_serializer import SerializerMixin
ModuleNotFoundError: No module named 'sqlalchemy_serializer'

I think the pipfile was missing the sqlalchemy_serializer package.

Additional Context

No response

Suggested Changes

No response

test only works with .get_json

Canvas Link

https://learning.flatironschool.com/courses/6182/assignments/209743?module_item_id=483942

Concern

The test will only pass if we use the .get_json() method in our post route. Should we tell students to default to using that? Is that how they'll basically always handle requests that come from a frontend? I had a similar question about this in a previous reading but I wanted to bring it up here, since this might delay students quite a bit since a lot of our examples use request.form.

Additional Context

No response

Suggested Changes

No response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.