Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get the data
#1
Hi I am new to python and I installed the requirements then I ran the main.py file then what I should do to browsse the get the json data ? here is the code:

import flask
import pandas as pd


def create_app():
    app = flask.Flask(__name__)

    @app.route('/', methods=['GET', 'POST'])
    def index():
        """
        Index page view handler.
        :return: rendered index.html template
        """
        return flask.render_template('index.html')

    @app.route('/data', methods=['GET', 'POST'])
    def data():
        """
        Data view handler
        :return: JSON object of the data CSV file
        """
        data = pd.read_csv('task_data.csv')

        context = {
            'sensor_data': data.to_dict(orient='list')
        }
        return flask.jsonify(context)

    return app


if __name__ == "__main__":
    app = create_app()
    # serve the application on port 7410
    app.run(host='0.0.0.0', port=7410)
Reply
#2
(Feb-13-2020, 06:17 PM)William369 Wrote:  def data():
        """
        Data view handler
        :return: JSON object of the data CSV file
        """
        data = pd.read_csv('task_data.csv')
 
        context = {
            'sensor_data': data.to_dict(orient='list')
        }
        return flask.jsonify(context)
 
    return app

You'll need to clarify. This here is you loading the file. I think if it's in the same folder it will load.

Where did you get this code? Do you understand it?
Reply
#3
Your import is not the normal way.
To do a test.
app.py
from flask import Flask, render_template, jsonify, request
import pandas as pd

app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
    """
    Index page view handler.
    :return: rendered index.html template
    """
    return render_template('index.html')

@app.route('/data', methods=['GET', 'POST'])
def data():
    """
    Data view handler
    :return: JSON object of the data CSV file
    """
    data = pd.read_csv('task_data.csv')

    context = {
        'sensor_data': data.to_dict(orient='list')
    }
    return jsonify(context)

if __name__ == '__main__':
   app.run(debug=True)
Run:
λ python app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 134-386-928
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Now in browser http://127.0.0.1:5000/ this will load index.html page,
you most have this file in templates folder.

To get data in browser http://127.0.0.1:5000/data
Output:
{ sensor_data: { 1: [ 4 ], 2: [ 5 ], 3: [ 6 ] } }
Test data i used task_data.csv
Output:
1,2,3 4,5,6
Reply


Forum Jump:

User Panel Messages

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