I am completely new to python language, if I am wrong please correct me.
This ia my file structure:
flask_webapp
├── __init__.py #empty file
├── app.py
└── test.py
Is it possible to import variables from flask source code in another python code?
'app.py' sorce code is:
from flask import Flask, request #import main Flask class and request object
app = Flask(__name__) #create the Flask app
@app.route('/json-example', methods=['POST']) #GET requests will be blocked
def json_example():
req_data = request.get_json()
language = None
if 'language' in req_data:
language = req_data['language'] #is this 'language' a variable?, If yes then why is it not getting imported?
framework = None
if 'framework' in req_data:
framework = req_data['framework']
python_version = None
if 'python_version' in req_data:
python_version = req_data['version_info']['python']
flask_version = None
if 'flask_version' in req_data:
flask_version = req_data['version_info']['flask']
example = None
if 'example' in req_data:
example = req_data['examples'][0]
boolean_test = None
if 'boolean_test' in req_data:
boolean_test = req_data['boolean_test']
return '''
The language value is: {}
The framework value is: {}
The Python version is: {}
The Flask version is: {}
The item at index 0 in the example list is: {}
The boolean value is: {}'''.format(language, framework, python_version, flask_version, example, boolean_test)
if __name__ == '__main__':
app.run(debug=True, port=5000) #run app in debug mode on port 5000
I want to import the variables "language" etc; from the flask code to my test.py module.
Code of 'test.py':
from pathlib import Path
from app import language, framework, python_version, flask_version, example, boolean_test
print('The value of language is %s here'%str(language))
But I get error:
Traceback (most recent call last): File "test.py", line 1, in from app import language, framework, python_version, flask_version, example, boolean_test ImportError: cannot import name 'language'
This is my jason data sending from Postman App and getting correct response back, so no isssue with flask webapp.
{
"language" : "Python",
"framework" : "Flask",
"website" : "Scotch",
"version_info" : {
"python" : 3.4,
"flask" : 0.12
},
"examples" : ["query", "form", "json"],
"boolean_test" : true
}
Comments
Post a Comment