0

Hi I am try to create a flask app to access various web APIs and display the information on different pages and import the data to my data base, I have been working on this for months, I am new to python, and nothing is simple to me, I am using flask blueprints, Here is my schema for the app:

etsy_api/
 |_ config.
 |_ etsy_api.py
 |_ db
 |_ app/
      |_ models.py
      | __init__.py "flask function factory with each bp registered"
      |_ auth/
             |___init__.py "registers blueprint"
             |_ forms.py "flask wtforms "class"
             |_ routes.py
      |_ erdm/
             |___init__.py "registers blueprint"
             |_ forms.py "flask wtforms "class"
             |_ routes.py
      |_templates/
             |_ base.html
             |_ auth/ "templates for auth"
             |_ erdm/
                   |_ Listings.html

I am following and adapting this tutorial here for a blog creation and I have found some etsy code here. The code looks very good, but I dont know how to incorperate it into my app! Do I copy this code into an etsy.py file in the app folder then call it in the erdm/routes.py file, and if so how? On the git site readme file it says; to call listings

e.show_listings(color='#FF00FF')

So how do I make a function of this? If not do I incorperate each function in the etsy file seperatly in the routes.py file as a seperate route (which is what I prefere). This leads to my second problem how do I but a button on the web page to call the function, How do I display the results in a table? Do i use flaskform? I tried to create my own code to request the information but I ran into a problem with the test, even though my code worked here is my code and here is the code that gets the response

from __future__ import print_function
from etsywrapper import Listings

active = Listings.active()

for number, show in enumerate(active['results'], start=1):
    print("{num}. {listing_id} - {title}".format(num=number,

    listing_id=show['listing_id'], title=show['title']))

this code prints the results that I want but I have know idea how to incorperate this into my app!

I hope there is somebody who understands my problem who can help guide me Regards paul

pascale
  • 145
  • 2
  • 13

1 Answers1

0

This is for all those like me that find even the most simplest of things in python really difficult. My code to find a list of listings in my Etsy shop and return just several parts of this list was origionally three files for my flask app I condensed init.py and __core.py, into one file __core.py.

import os
import requests

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

and then the really hard part which I really didnt understand, and that was how to write a function to make the request to the Etsy API server, I had some code

from __future__ import print_function
from etsywrapper.__core import Listings

active = Listings.active()

for number, show in enumerate(active['results'], start=1):
    print("{num}. {listing_id} - {title}".format(num=number,
                                             listing_id=show['list

that worked but I needed a view function so after lots of head scratching Here it is

from app.etsy.__core import Listings

@bp.route('/listings')
def listings():
    active = Listings.active()
    for number, show in enumerate(active['results'], start=1):
        return("{num}. {listing_id} - {title}".format(num=number,
                                             listing_id=show['listing_id'], 
                                              title=show['title']))

I understood it would need "def 'name'()" and then some code! but does the name matter? and what code? Now I know, Any name that is relevent to the function and code that works, I had to change "print" to "return" but now I have some information on a web page and NNNNNNNNNo Error messages. One small step for a noob, but a huge leap in my knowledge

pascale
  • 145
  • 2
  • 13