pythonjsonapiiotsmart-device

Parse result into JSON using Python and Flask


Now, I managed to successfully pull basic information from my smart device onto the terminal using pyHS100 on python (v3.6) using the following code

from pyHS100 import SmartPlug
from pprint import pformat as pf
plug = SmartPlug("10.xxx.xxx.xxx")
print("Hardware: %s" % pf(plug.hw_info))

which results in the following:

enter image description here

but I can't parse the data into json format and display it on the local server for my RESTful API purpose if I done it this way:

from flask import Flask, jsonify
from flask_restful import Resource, Api
from pyHS100 import SmartPlug

app = Flask(__name__)
@app.route('/api')
def get():
    plug = SmartPlug("10.xxx.xxx.xxx")
    sys = plug.hw_info
    return jsonify({'data':sys})

if __name__ == '__main__':
    app.run(host='0.0.0.0')
    app.run(debug=True)

All I need is for the information to be presented into something like this:

enter image description here

enter image description here What did I do wrong and how do I this fix? Thanks


Solution

  • I believe the best way to solving this is by using json.dumps