Python Forum
flask requests display data from api on webpage with javacript
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
flask requests display data from api on webpage with javacript
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can users store data temporarily in flask app? darktitan 6 2,935 Mar-21-2022, 06:38 PM
Last Post: darktitan
  POST requests - different requests return the same response Default_001 3 1,938 Mar-10-2022, 11:26 PM
Last Post: Default_001
  Help using Flask for a countdown on webpage ZenBuddhism 1 2,220 Feb-05-2021, 03:35 PM
Last Post: snippsat
  Using range slider in flask webpage to use in python KimPet 2 7,615 Jan-23-2021, 11:58 PM
Last Post: snippsat
  Flask, Display a picture outisde the static SpongeB0B 6 15,470 Aug-29-2020, 05:15 PM
Last Post: nilamo
  Extract data from a webpage cycloneseb 5 2,879 Apr-04-2020, 10:17 AM
Last Post: alekson
  Flask, Posgresql - Multiple requests are not working bmaganti 5 2,747 Feb-20-2020, 03:02 PM
Last Post: bmaganti
  how to save the data from MySQL to CSV in Flask farah97 4 2,928 Jan-03-2020, 03:02 AM
Last Post: farah97
  Using flask to add data to sqlite3 table with PRIMARY KEY catafest 1 3,725 Sep-09-2019, 07:00 AM
Last Post: buran
  Display module variables and edit through webpage freak14 1 2,058 Aug-08-2019, 06:26 AM
Last Post: fishhook

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020