Another "SyntaxError: 'return' outside function" for Flask Application

383 views Asked by At

Im trying to get over this beginners rut and wanted to see what the community would suggest.

Photo #1 CODE ON SPYDER IDE

Photo #2 SyntaxError ON SPYDER CONSOLE

The goal of this project is to relay the extracted data from an OBDII device to a flask application so the data can be displayed (using Postman to test script)

I'm having difficulty figuring out how my 'return' statement landed outside my function and the fixes I need to do to move past this.

All help is deeply appreciated :)

import obd
from flask import Flask, jsonify
import json

#Creating a Web App 
app = Flask(__name__)

#Extracting OBDII Data (RPM for now)
@app.route('/Extract_Data', methods=['GET'])

def Extract_Data():
    obd.logger.setLevel(obd.logging.DEBUG)
connection=obd.OBD()
rpm=obd.commands.RPM
response=connection.query(rpm)
connection.close()
return jsonify(response.value),200

#Running The App 
app.run(host = '0.0.0.0', port = 5000)
1

There are 1 answers

0
KetZoomer On BEST ANSWER

Indent the code in your function. Python relies heavily on indentation, so you have to indent any code inside a function.

import obd
from flask import Flask, jsonify
import json

# Creating a Web App 
app = Flask(__name__)

# Extracting OBDII Data (RPM for now)
@app.route('/Extract_Data', methods=['GET'])
def Extract_Data():
    obd.logger.setLevel(obd.logging.DEBUG)
    connection=obd.OBD()
    rpm=obd.commands.RPM
    response=connection.query(rpm)
    connection.close()
    return jsonify(response.value), 200

# Running The App 
app.run(host = '0.0.0.0', port = 5000)

Also, on this line: return jsonify(response.value), 200, 200 is the default status code so you could use return jsonify(response.value) instead.