30
loading...
This website collects cookies to deliver better user experience
You can jump to the final result of this tutorial by forking this Repl →
db_url
and for the value enter the connection string that Sequin provided earlier. Then click the Add new secret button:You can easily retrieve the string for your Sequin database by opening the Sequin Console and clicking the Connect button on your Product Catalogue resource).
airtable_api_key
(which you retrieved earlier) and your airtable_base_id
(which you can find in the Airtable API docs by selecting your base).main.py
with the following:from flask import Flask, render_template, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
import requests
import json
import os
web_site = Flask(__name__)
web_site.config[
'SQLALCHEMY_DATABASE_URI'] = os.environ['db_url']
db = SQLAlchemy(web_site)
os.environ['db_url']
).main.py
file:...
class Furniture(db.Model):
id = db.Column(db.Integer, primary_key=True)
unit_cost = db.Column(db.Float, unique=False, nullable=False)
in_stock = db.Column(db.Boolean, unique=False, nullable=True)
name = db.Column(db.String(100), unique=True, nullable=False)
images = db.Column(db.ARRAY(db.String), unique=False, nullable=False)
description = db.Column(db.String(400), unique=False, nullable=False)
def __repr__(self):
return '<Product %r>' % self.name
main.py
file.@web_site.route('/')
def index():
products = Furniture.query.all()
return render_template('index.html',products=products)
@web_site.route('/product/<productid>')
def view_product(productid):
product = Furniture.query.filter_by(id=productid).first()
return render_template('product.html', product=product)
@web_site.route('/delete/<productid>')
def delete_product(productid):
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {0}'.format(os.environ['airtable_api_key'])
}
response = requests.delete(
"https://proxy.sequin.io/api.airtable.com/v0/{base_id}/Furniture/{product_id}".format(
base_id = os.environ['airtable_base_id'],
product_id = productid),
headers=headers)
print(response.content)
return redirect(url_for('index'))
main.py
file to run the webserver on localhost:8080
:if __name__ == '__main__':
web_site.run(host='0.0.0.0', port=8080)
index.html
file with the following:<!DOCTYPE html>
<html>
<head>
<title>Product Catalog from Airtable</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x"
crossorigin="anonymous"
/>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4"
crossorigin="anonymous"
></script>
</head>
<body>
<div class="container">
<h1>Welcome to this product catalog demo from Airtable</h1>
{% for product in products %}
<div class="row">
<div class="col-md-4">
<img
src="{{product.images[0]}}"
alt="{{product.name}}"
width="100px;"
/>
</div>
<div class="col-md-2">Name: {{product.name}}</div>
<div class="col-md-2">Unit cost: $ {{product.unit_cost}}</div>
<div class="col-md-2">
<a href="/product/{{product.id}}" class="btn btn-success"
>View details</a
>
</div>
</div>
<br />
<br />
{% endfor %}
</div>
</body>
</html>
product.html
and add the following:<!DOCTYPE html>
<html>
<head>
<title>Product</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x"
crossorigin="anonymous"
/>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4"
crossorigin="anonymous"
></script>
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1>{{product.name}}</h1>
<p class="lead">{{product.description}}</p>
</div>
<div class="row">
<div class="col-md-5">
{% for image in product.images %}
<img src="{{image}}" alt="image" class="img-fluid" />
{% endfor %}
</div>
<div class="col-md-6">
<p>Unit cost: <span>$ {{product.unit_cost}}</span></p>
<p>Is in stock?: <span>{{product.in_stock}}</span></p>
</div>
</div>
<a href="/" class="btn btn-primary"> Back </a>
<a href="/delete/{{product.id}}" class="btn btn-danger"> Delete </a>
</div>
</body>
</html>