I sucessfully pushed an application to Cloud Foundry on IBM Cloud. When I visit the app url it is showing as name errror.
this is how my code looks like
import numpy as np
import json
import os
import re
import ibm_boto3
import ibm_db
import msgpack
from ibmdbpy import IdaDataBase, IdaDataFrame
from ibm_botocore.client import Config, ClientError
from flask_cors import CORS
import pandas as pd
from matplotlib import pyplot as plt
from flask import Flask, jsonify, json, request
from sklearn.cluster import KMeans
import requests
from io import StringIO
import csv`enter code here`
@app.route("/", methods=["GET"])
def hello():
global age
global gender
title = ["Stud", "Hanging", "Hoop", "Nosestud", "Nosehoop", "Barbell"]
# prepare the data
if 'age' in request.args:
age = int(request.args['age'])
if 'gender' in request.args:
gender = request.args['gender']
f0 = data['Stud'].values
f0 = np.append(f0, 0)
f1 = data['Gender'].values
f1 = np.append(f1, gender)
f2 = data['Age'].values
f2 = np.append(f2, age)
f3 = data['Hanging'].values
f3 = np.append(f3, 0)
f5 = data['Hoop'].values
f5 = np.append(f5, 0)
f6 = data['Nosestud'].values
f6 = np.append(f6, 0)
f7 = data['Nosehoop'].values
f7 = np.append(f7, 0)
f8 = data['Barbell'].values
f8 = np.append(f8, 0)
f4 = [ord(n) for n in f1]
Y = np.array(list(zip(f0, f3, f5, f6, f7, f8, f4, f2)))
# cluster the data using KMeans Algorithm
kmeans = KMeans(max_iter=50, n_clusters=10)
kmeans = kmeans.fit(Y)
labels = kmeans.predict(Y)
centroids = kmeans.cluster_centers_
counts = {0: {"Stud": 0, "Hanging": 0, "Hoop": 0, "Nosestud": 0, "Nosehoop": 0, "Barbell": 0}, 1: {"Stud": 0, "Hanging": 0, "Hoop": 0, "Nosestud": 0, "Nosehoop": 0, "Barbell": 0}, 2: {"Stud": 0, "Hanging": 0, "Hoop": 0, "Nosestud": 0, "Nosehoop": 0, "Barbell": 0}, 3: {"Stud": 0, "Hanging": 0, "Hoop": 0, "Nosestud": 0, "Nosehoop": 0, "Barbell": 0}, 4: {"Stud": 0, "Hanging": 0, "Hoop": 0, "Nosestud": 0, "Nosehoop": 0, "Barbell": 0}, 5: {
"Stud": 0, "Hanging": 0, "Hoop": 0, "Nosestud": 0, "Nosehoop": 0, "Barbell": 0}, 6: {"Stud": 0, "Hanging": 0, "Hoop": 0, "Nosestud": 0, "Nosehoop": 0, "Barbell": 0}, 7: {"Stud": 0, "Hanging": 0, "Hoop": 0, "Nosestud": 0, "Nosehoop": 0, "Barbell": 0}, 8: {"Stud": 0, "Hanging": 0, "Hoop": 0, "Nosestud": 0, "Nosehoop": 0, "Barbell": 0}, 9: {"Stud": 0, "Hanging": 0, "Hoop": 0, "Nosestud": 0, "Nosehoop": 0, "Barbell": 0}}
prod = {0: [], 1: [], 2: [], 3: [], 4: [],
5: [], 6: [], 7: [], 8: [], 9: []}
# list all the products that are purchased by all the users, cluster wise
for record in range(0, 217):
cluster_id = labels[record]
for attr in range(len(title)):
if(Y[record][attr] == 1):
current_products = title[attr]
counts[cluster_id][title[attr]
] = counts[cluster_id][title[attr]]+1
lst = prod[cluster_id]
if current_products not in lst:
lst.append(current_products)
prod[cluster_id] = lst
# delete entries of products from counts that hasn't been purchased even once,cluster wise
for cluster in range(10):
for attr in title:
if(counts[cluster][attr] == 0):
del counts[cluster][attr]
# identify the cluster of new user and pass the json object that has recommended products
label = labels[217]
# dict1={"type":"","category":0,"count":0,"products":""}
product_details = {"products": "", "type": "", "count": 0, "category": 0}
count_keys = []
count_values = []
recommended_products = []
for id in range(10):
if(label == id):
for attr in counts[id].keys():
count_keys.append(attr)
count_values.append(counts[id][attr])
for key in range(len(count_keys)):
product_details["category"] = count_keys[key]
product_details["count"] = count_values[key]
if(count_keys[key] == "Stud" or count_keys[key] == "Hanging" or count_keys[key] == "Hoop"):
product_details["type"] = "Ear"
if(count_keys[key] == "Stud"):
product_details["products"] = stud_list
elif(count_keys[key] == "Hanging"):
product_details["products"] = hanging_list
elif(count_keys[key] == "Hoop"):
product_details["products"] = hoop_list
elif(count_keys[key] == "Nosestud" or count_keys[key] == "Nosehoop" or count_keys[key] == "Barbell"):
product_details["type"] = "Nose"
if(count_keys[key] == "Nosestud"):
product_details["products"] = nose_stud_list
elif(count_keys[key] == "Nosehoop"):
product_details["products"] = nose_hoop_list
elif(count_keys[key] == "Barbell"):
product_details["products"] = barbell_list
recommended_products.append(product_details.copy())
jsonStr = json.dumps(recommended_products)
return jsonify(json.loads(jsonStr))
port = os.getenv('VCAP_APP_PORT', '8080')
if _name_ == '_main_':
app.run(debug=True, host='0.0.0.0', port=port)
# app.run(debug=True)
The error that i am getting looks like below
NameError NameError: name 'gender' is not defined Traceback (most recent call last) File "/home/vcap/deps/0/python/lib/python3.8/site-packages/flask/app.py", line 2309, in __call__ return self.wsgi_app(environ, start_response) File "/home/vcap/deps/0/python/lib/python3.8/site-packages/flask/app.py", line 2295, in wsgi_app response = self.handle_exception(e) File "/home/vcap/deps/0/python/lib/python3.8/site-packages/flask_cors/extension.py", line 161, in wrapped_function return cors_after_request(app.make_response(f(*args, **kwargs))) File "/home/vcap/deps/0/python/lib/python3.8/site-packages/flask/app.py", line 1741, in handle_exception reraise(exc_type, exc_value, tb) File "/home/vcap/deps/0/python/lib/python3.8/site-packages/flask/_compat.py", line 35, in reraise raise value File "/home/vcap/deps/0/python/lib/python3.8/site-packages/flask/app.py", line 2292, in wsgi_app response = self.full_dispatch_request() File "/home/vcap/deps/0/python/lib/python3.8/site-packages/flask/app.py", line 1815, in full_dispatch_request rv = self.handle_user_exception(e) File "/home/vcap/deps/0/python/lib/python3.8/site-packages/flask_cors/extension.py", line 161, in wrapped_function return cors_after_request(app.make_response(f(*args, **kwargs))) File "/home/vcap/deps/0/python/lib/python3.8/site-packages/flask/app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb) File "/home/vcap/deps/0/python/lib/python3.8/site-packages/flask/_compat.py", line 35, in reraise from io import StringIO def reraise(tp, value, tb=None): if value.__traceback__ is not tb: raise value.with_traceback(tb) raise value implements_to_string = _identity else: text_type = unicode File "/home/vcap/deps/0/python/lib/python3.8/site-packages/flask/app.py", line 1813, in full_dispatch_request rv = self.dispatch_request() File "/home/vcap/deps/0/python/lib/python3.8/site-packages/flask/app.py", line 1799, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/home/vcap/app/KMeans_200.py", line 200, in hello f1 = np.append(f1, gender) NameError: name 'gender' is not defined The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error. To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side. You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection: dump() shows all variables in the frame dump(obj) dumps all that's known about the object