Python Forum

Full Version: flask requests display data from api on webpage with javacript
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi I am building a web app to access my Etsy store, so that i can download stock, sales, messages etc, and display this information and save what I need to the data base, I am following the flask tutorial and miguel grindberg translate section, these are for a blog, so I am adapting their code. I have set up an app factory, and blueprints, I have created code to access the Etsy api and retrieve the required data. I want to have a button on the web page, and a table to display the returned data with checkboxs on one side so I can choose what data to save then a second button to then save the chosen data to database. Here is my code:
#etsy.py
from __future__ import print_function
from flask import (
    Blueprint, flash, g, redirect, render_template, request, session, url_for
)
from werkzeug.exceptions import abort
from app.auth.auth import login_required
from app.db import get_db
from app.etsy.__core import Listings

bp = Blueprint('etsy', __name__, url_prefix='/etsy')

@bp.route('/sales', methods=('GET', 'POST'))
@login_required
def sales():
    db = get_db()
    bbsales = db.execute(
        'SELECT sdate, pseudo, description, qt, prix, total FROM bbsales'
        
    ).fetchall()
    return render_template('etsy/sales.html', bbsales=bbsales)

@bp.route('/listings', methods=('GET', 'POST'))
@login_required
def listings():
    if request.method == 'POST':
        listing_id = request.form['listing_id']
        title = request.form['title']
        sku = request.form['sku']
        price = request.form['price']
        error = None
        
    return render_template('etsy/listings.html')
Here is the html template.

{% extends 'base.html' %}

{% block header %}
   <h1>{% block title %}Etsy{% endblock %}</h1>
  {% if g.user %}
    <a class="action" href="{{ url_for('etsy.sales') }}">new sales</a>
    <a class="action" href="{{ url_for('etsy.listings') }}">listings</a>
  {% endif %}
{% endblock %}

{% block content %}
   <h1>Listings</h1>
   <span id="listings{{ li.id }}">
                                   </span>

   {% block scripts %}
       <script>
           function get_listings() {
               active = Listings.active()
    for number, show in enumerate(active['results'], start=1):
        return("{num} {listing_id} {title} {sku} {price}".format(num=number,
                                                 listing_id=show['listing_id'], title=show['title'],
                                                 sku=show['sku'], price=show['price']))
           }
       </script>
    {% endblock %}
    
{% endblock %}
And here is the code for the Etsy module __core.py
import os
import requests
import json
ERDM_API_KEY = os.environ.get('ERDM_API_KEY', None)

class Listings(object):
    
    def __init__(self, id):
        self.id = id
    
    def info(self):
        path = 'https://openapi.etsy.com/v2/listings/{}/inventory'.format(self.id)
        response = session.get(path)
        return response.json()
    
    @staticmethod
    def active():
        path = 'https://openapi.etsy.com/v2/shops/ERDMHardware/listings/active'
        response = session.get(path)
        return response.json()

class APIKeyMissingError(Exception):
    pass

if ERDM_API_KEY is None:
    raise APIKeyMissingError(
        "All methods require an API key. "
    )
session = requests.Session()
session.params = {}
session.params['api_key'] = ERDM_API_KEY
There is code missing from the html template and the viex function in etsy.py, because I have no Idea how to move forward, I do not understand how to convert my code into code that returns the data on to the webpage. I would like to display the results in a table after <h1>Listings</h1>.
The function 'get_listings' returns four fields from a list of 20 fields, pagination of 1 page of 25 results,
If somebody could please help me with the code to enable me to display these four results I can then work out how to add the check box, and then I will need help on how to return only the chosen rows to the database.
It all sounds really easy, but I need help!!
regards
Paul