Skip to main content

Add a shopping cart to your Flask app

Project description

Flask-Shoppingcart

Flask-Shoppingcart is an extension to help you to build a simple shopping-cart to your e-commerce or anything that needs a shopping-cart usage in your Flask application.

Instalation

Install the extension with pip:

$ pip install flask-shoppingcart 

Find Flask-Shoppingcart at PyPI

A Basic Example

Let's walk through setting up a basic application. Note that this is a very basic guide: we will be taking shortcuts here that you should never take in a real application.

To begin we'll set up a Flask app and a FlaskShoppingCart from Flask-Shoppingcart.

import flask
from flask_shoppingcart import FlaskShoppingCart

app = flask.Flask(__name__)
app.secret_key = "super secret string"  # Change this!

shoppingcart = FlaskShoppingCart(app)

Then we will be able to manage our shopping cart from it:

@app.route("/")
def example_route():
    my_product_id = 1  # this could be a query to the database, get by a query-param in the URL or something like that
    
    # Adding a product to the cart
    shopping_cart.add(my_product_id, quantity=5)  # the quantity is 1 by default

    # Subtracting a specific quantity from the cart 
    shopping_cart.subtract(my_product_id, quantity=3) # Now the quantity in the cart for this product should be 2

    # Removing it from the cart. Other products in the cart will ramain unmodified
    shopping_cart.remove(my_product_id)

    # Removing all items from the cart, the cart now is empty
    shopping_cart.clear()

    return "wow, this is awesome!"

API Reference

If you are looking for information on a specific function, class or method, this part of the documentation is for you.

we will be taking shortcuts here that you should never take in a real application or take some things for granted (like imports).

The FlaskShoppingCart class

The FlaskShoppingCart is the main class that you will use to manage your cart throughout your entire application, with it you will be able to add, subtract, remove and clear your user's carts, along with other methods that you might add.

You can instance it as a simple Flask application:

from flask import Flask
from flask_shoppingcart import FlaskShoppingCart

app = Flask(__name__)
shopping_cart = FlaskShoppingcart(app)

or as an advanced one:

from flask import Flask
from flask_shoppingcart import FlaskShoppingCart

app = Flask(__name__)
shopping_cart = FlaskShoppingcart(app)

def create_app():
    shopping_cart.init_app(app)

    return app

and then use it as others extensions

from app import shopping_cart

@app.route("/")
def my_route():
    shopping_cart.add(1)

    return jsonify(shopping_cart.cart)

In order to work with the application's cookies, FlaskShoppingCart adds an after_request to the application to apply the modified and/or created cookie to manage the cart, otherwise the extension would not be able to manage the products in the user's cart.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_shoppingcart-0.2.0.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_shoppingcart-0.2.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file flask_shoppingcart-0.2.0.tar.gz.

File metadata

  • Download URL: flask_shoppingcart-0.2.0.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.5

File hashes

Hashes for flask_shoppingcart-0.2.0.tar.gz
Algorithm Hash digest
SHA256 379e90ff46b5d67d179b4f833b456f70046400c9a7a1efe8332fa9f0d1ef1d06
MD5 fcb2c1a25ebb0bc85344dfa73e963524
BLAKE2b-256 9957d68ca6b5065214d4fb070e2d24ba21202db788da5e6920c340391c6a2096

See more details on using hashes here.

File details

Details for the file flask_shoppingcart-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_shoppingcart-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cb69511dda7b4f7fafb8bfec694b8cb73a9ac7f4d2d21fb0fb665b0cc001df56
MD5 e10f0c3f21610ab19db5ed3695720879
BLAKE2b-256 f9e41b6f43b831f8e04049a8cbc851773f7e56d664e9b61767d488d99c94bb1e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page