简体   繁体   中英

How to debug a flask-restful api with pdb

I want to use pdb to step into some flask-restful code. I have an endpoint which returns a token. I then use the token to access another endpoint which returns the required data. I would like to view the result of a database query. How do I go about this?

I tried setting a breakpoint inside the class, but it does not get triggered when I send a request using the request library.

class FetchData(Resource):

    @jwt_required
    def get(self, args):

        engine = create_engine('mysql+pymysql://')
        conn = engine.connect()

        tablemeta = MetaData()
        tablemeta.reflect(bind=engine)

        keydate = tablemeta.tables['KEYDATE']
        coefficient = tablemeta.tables['COEFFICIENT']
        vessel = tablemeta.tables['VESSEL']
        update_dict = {}


        s = select([coefficient])
        s = s.where(coefficient.c.updated_date >= args["dt"])
        rp = conn.execute(s)

        result = []

        for r in rp:
            j = coefficient.join(vessel, r['idvessel'] == vessel.c.idvessel)

            import pdb
            pdb.set_trace()
            vdm_id = select([vessel.c.vessel_id]).select_from(j)
            vdm_id = conn.execute(vdm_id).scalar()

            intermediate = []
            intermediate.append({"vdm_id": vdm_id})
            intermediate.append([dict(r)])

            result.append(intermediate)

Or possibly there's another debugger I should be using?

You should put your pdb before the loop as it will never get to pdb if you don't get any results.

I have been using pdb for the last few years in flask without any problems.

只需使用print(随心所欲),这应该更快,更高效。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM