Foodtech Swish closes $38M Series B at $139M valuation
Back to Tutorials
techTutorialbeginner

Foodtech Swish closes $38M Series B at $139M valuation

March 23, 202618 views5 min read

Learn how to build a basic food delivery application using Python and Flask, mimicking the technology used by companies like Swish.

Introduction

In the rapidly growing foodtech industry, companies like Swish are revolutionizing how we think about fast food delivery by integrating technology, kitchen operations, and logistics. This tutorial will guide you through building a basic food delivery application using Python and Flask, mimicking some of the core technology concepts that companies like Swish use to scale their operations. By the end of this tutorial, you'll have a working prototype of a food delivery app that handles orders, tracks delivery status, and integrates with a simple database.

Prerequisites

To follow along with this tutorial, you'll need:

  • A basic understanding of Python programming
  • Python 3.6 or higher installed on your computer
  • Flask web framework installed (we'll cover this in detail)
  • A text editor or IDE (like VS Code or PyCharm)
  • Basic knowledge of HTML and REST APIs

This tutorial assumes no prior experience with Flask or web development, so we'll start from the ground up.

Step-by-Step Instructions

1. Set Up Your Development Environment

First, we need to install Flask, which is a lightweight web framework for Python. Open your terminal or command prompt and run the following command:

pip install Flask

This command installs Flask and its dependencies. Flask makes it easy to create web applications in Python and is commonly used in startups like Swish for rapid prototyping and development.

2. Create Your Flask App Structure

Next, we'll create a basic directory structure for our Flask app. Create a new folder called food_delivery_app and inside it, create the following files:

  • app.py - This will be our main application file
  • database.py - For managing our data
  • templates/ - For HTML templates
  • static/ - For CSS, JS, and images

Inside the templates folder, create a file called index.html.

3. Create a Basic Flask Application

Open app.py and add the following code:

from flask import Flask, render_template, request, jsonify

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)

This code initializes a Flask app, sets up a route for the homepage, and runs the app in debug mode. Debug mode is helpful for development as it shows errors directly in the browser.

4. Create a Simple HTML Template

Inside templates/index.html, add the following HTML code:

<!DOCTYPE html>
<html>
<head>
    <title>Swish Food Delivery</title>
</head>
<body>
    <h1>Welcome to Swish Food Delivery</h1>
    <p>Order your favorite food and get it delivered fast!</p>
</body>
</html>

This is a basic HTML page that will be rendered when users visit the homepage. As we build out the app, we'll add more functionality.

5. Set Up a Simple Database

Open database.py and add the following code to create a simple in-memory database for our orders:

orders = []

def add_order(order_data):
    order_id = len(orders) + 1
    order = {
        'id': order_id,
        'customer_name': order_data['customer_name'],
        'items': order_data['items'],
        'status': 'preparing'
    }
    orders.append(order)
    return order


def get_order(order_id):
    for order in orders:
        if order['id'] == order_id:
            return order
    return None


def update_order_status(order_id, status):
    for order in orders:
        if order['id'] == order_id:
            order['status'] = status
            return order
    return None

This simple database simulates how Swish might store order information. In a real application, you'd connect this to a database like PostgreSQL or MongoDB.

6. Add API Endpoints for Orders

Now, let's update app.py to include API endpoints for managing orders:

from flask import Flask, render_template, request, jsonify
from database import add_order, get_order, update_order_status

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/api/orders', methods=['POST'])
def create_order():
    data = request.get_json()
    order = add_order(data)
    return jsonify(order), 201

@app.route('/api/orders/', methods=['GET'])
def get_order_by_id(order_id):
    order = get_order(order_id)
    if order:
        return jsonify(order)
    return jsonify({'error': 'Order not found'}), 404

@app.route('/api/orders//status', methods=['PUT'])
def update_status(order_id):
    data = request.get_json()
    order = update_order_status(order_id, data['status'])
    if order:
        return jsonify(order)
    return jsonify({'error': 'Order not found'}), 404

if __name__ == '__main__':
    app.run(debug=True)

This code adds three API endpoints:

  • POST /api/orders - Creates a new order
  • GET /api/orders/<id> - Retrieves an order by ID
  • PUT /api/orders/<id>/status - Updates the status of an order

These endpoints simulate how Swish might manage order data and status updates in real-time.

7. Test Your Application

Run your Flask app by executing the following command in your terminal:

python app.py

Visit http://127.0.0.1:5000 in your browser to see the homepage. To test the API endpoints, you can use a tool like Postman or curl. For example, to create an order:

curl -X POST http://127.0.0.1:5000/api/orders \
  -H 'Content-Type: application/json' \
  -d '{"customer_name": "John Doe", "items": ["Pizza", "Burger"]}'

This command sends a POST request to create a new order. You can then use GET and PUT requests to retrieve and update the order status.

Summary

In this tutorial, we've built a basic food delivery application using Python and Flask. We've covered setting up the development environment, creating a simple database, and implementing API endpoints for managing orders. This prototype mimics some of the core technologies used by companies like Swish to scale their delivery operations.

While this is a simplified version, it demonstrates the fundamental concepts of how modern foodtech startups build scalable applications. As you continue to develop your app, you can add features like user authentication, real-time delivery tracking, and integration with payment systems.

Source: TNW Neural

Related Articles