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.1.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.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask_shoppingcart-0.2.1.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.1.tar.gz
Algorithm Hash digest
SHA256 a4f4027fb71379c3f1b4e800178a508146b0df5c3f640b6a417abfb05e4bc471
MD5 363c117b5ae1e56ad9b695aac69a2d56
BLAKE2b-256 8d2955a75c21e7a18422784deb0b75bd264f67f8e0bf47c0cda9379e224ff4bc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flask_shoppingcart-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c3dd6b0e37f436128018be46b1d057c38e033722ebb8c507d7c259db7ddc1abb
MD5 b98745ce7b2af7bbf7bef03e1b456577
BLAKE2b-256 f70db127a27366e4fa7278abe1d84950c0b93b5366af33d050f1997646737c0a

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