如何让这个 Flask API return 成为一个值?

How do I make this Flask API return a value?

我有一个 Python Flask 脚本,其中包含我开发的 JSON。 我正在尝试 return 使用此 url 的城市:http://127.0.0.1:5000/restaurant/city.

我收到这个错误:

Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

为什么我的代码逻辑错误?

我正在使用 Python 3.8 和 Flask。

这是我的代码(JSON 在代码中):

from flask import Flask, jsonify, request
import json

# from urllib3 import request
# import urllib.request as urllib

app = Flask(__name__)
restaurant = [
    {"city": "Al-Khobar",
     "details": [
         {
             "id": 1,
             "Area": "Near Corniche",
             "Restaurant": "Naranj Damascus Restaurant",
             "Street": "Firas Bin Al Nadr Street",
             "location link": "https:\/\/www.tripadvisor.com\/Restaurant_Review-g298545-d22895043-Reviews-Naranj_Damascus_Restaurant-Al_Khobar_Eastern_Province.html#MAPVIEW",
             "City": "Al Khobar",
             "Zip": 34447,
             "Country": "Saudi Arabia",
             "phone": 966508446622,
             "meals": "Breakfast, Lunch, Dinner, Late Night",
             "cuisine": "International, Barbecue, Grill, Diner, Middle Eastern",
             "price range": "' to "
         },

         {
             "id": 2,
             "Area": "Near Corniche",
             "Restaurant": "The Butcher Shop and Grill",
             "Street": "Prince Turkey Street",
             "location link": "https:\/\/www.tripadvisor.com\/Restaurant_Review-g298545-d10691837-Reviews-The_Butcher_Shop_and_Grill-Al_Khobar_Eastern_Province.html#MAPVIEW",
             "City": "Al Khobar",
             "Zip": " ",
             "Country": "Saudi Arabia",
             "phone": 966138085182,
             "meals": "Lunch, Dinner",
             "cuisine": "Steakhouse",
             "price range": "' to "
         },
         {
             "id": 3,
             "Area": "Near Corniche",
             "Restaurant": "Kosebasi, Al Khobar",
             "Street": "Prince Turkey Street",
             "location link": "https:\/\/www.tripadvisor.com\/Restaurant_Review-g298545-d9874670-Reviews-Kosebasi_Al_Khobar-Al_Khobar_Eastern_Province.html#MAPVIEW",
             "City": "Al Khobar",
             "Zip": " ",
             "Country": "Saudi Arabia",
             "phone": 966138030089,
             "meals": "Lunch, Dinner",
             "cuisine": "Turkish, Middle Eastern, Barbecue",
             "price range": "' to "
         },
         {
             "id": 4,
             "Area": "Near Corniche",
             "Restaurant": "Bun & Patty",
             "Street": "Prince Turkey Street Al Yarmouk",
             "location link": "https:\/\/www.tripadvisor.com\/Restaurant_Review-g298545-d8054714-Reviews-Bun_Patty-Al_Khobar_Eastern_Province.html#MAPVIEW",
             "City": "Al Khobar",
             "Zip": 344233213,
             "Country": "Saudi Arabia",
             "phone": " ",
             "meals": "Lunch, Dinner",
             "cuisine": "American, Fast Food",
             "price range": " "
         }
     ]
     },

    {"city": "Dammam",
     "details": [
         {
             "id": 5,
             "Area": "Near Corniche",
             "Restaurant": "Abu Nawas",
             "Street": "Al Adama – Prince Mansour Street",
             "location link": "https://www.tripadvisor.com/Restaurant_Review-g298547-d805085-Reviews-Abu_Nawas-Dammam_Eastern_Province.html#MAPVIEW",
             "City": "Dammam",
             "Zip": "31461",
             "Country": "Saudi Arabia",
             "phone": 966138266363,
             "meals": " ",
             "cuisine": "Lebanese, Mediterranean, Middle Eastern",
             "price range": "' to "
         },
         {
             "id": 6,
             "Area": "Near Corniche",
             "Restaurant": "Heritage Village",
             "Street": "Prince Turkey Street",
             "location link": "https://www.tripadvisor.com/Restaurant_Review-g298547-d805123-Reviews-Heritage_Village-Dammam_Eastern_Province.html#MAPVIEW",
             "City": "Dammam",
             "Zip": " ",
             "Country": "Saudi Arabia",
             "phone": 96638090000,
             "meals": "Lunch, Dinner",
             "cuisine": "Middle Eastern, Vegetarian Friendly, Halal",
             "price range": "' to "
         },
         {
             "id": 7,
             "Area": "Near Corniche",
             "Restaurant": "Manoosha Alreef",
             "Street": "Prince Faisal Bin Fahad Road Khobar North",
             "location link": "https://www.tripadvisor.com/Restaurant_Review-g298547-d10221865-Reviews-Manoosha_Alreef-Dammam_Eastern_Province.html#MAPVIEW",
             "City": "Dammam",
             "Zip": "34426",
             "Country": "Saudi Arabia",
             "phone": 966539222673,
             "meals": "Breakfast, Lunch, Dinner",
             "cuisine": "Bakeries, Lebanese, Fast Food",
             "price range": "' to "
         },
         {
             "id": 8,
             "Area": "Near Corniche",
             "Restaurant": "American, Steakhouse",
             "Street": "Prince Mohammad Bin Fahad St.",
             "location link": "https://www.tripadvisor.com/Restaurant_Review-g298547-d2659493-Reviews-Steak_House-Dammam_Eastern_Province.html#MAPVIEW",
             "City": "Dammam",
             "Zip": 11372,
             "Country": "Saudi Arabia",
             "phone": "96638335468",
             "meals": "Lunch, Dinner",
             "cuisine": "American, Fast Food",
             "price range": " to "
         }
     ]
     }
]


@app.route('/restaurant')
def hello():
    return jsonify(restaurant)


@app.route('/restaurant/city')
def places():
    for x in jsonify(restaurant):
        if x[jsonify(restaurant)] == 'city':
            return x
    return {'city': None}
    # return jsonify(restaurant['city'])


@app.route('/restaurant', methods=['POST'])
def add_subscription():
    subscription = request.get_json(force=True)
    restaurant.append(subscription)
    return {'id': len(restaurant)}, 200


@app.route('/restaurant/<int:index>', methods=['PUT'])
def update_subscription(index):
    subscription = request.get_json(force=True)
    restaurant[index] = subscription
    return jsonify(restaurant[index]), 200


@app.route('/restaurant/<int:index>', methods=['DELETE'])
def delete_subscription(index):
    restaurant.pop(index)
    return 'None', 200


app.run()

代码的一些问题。

  1. 您提供的restaurant变量不需要“jsonified”,您可以按原样使用它。
  2. 路径 /restaurant/city 应该有 city 作为用户的变量,要实现这一点,您需要如下声明装饰器。现在“城市”也是该方法的参数:
@app.route('/restaurant/<city>')
def places(city):

然后我猜你正试图将变量 city 与字典中的城市进行匹配,因此整个代码将变为:

@app.route('/restaurant/<city>')
def places(city):
    for x in restaurant:
        if x["city"] == city:
            return x
    return {'city': None}

要测试代码,您应该使用 URL,例如:http://127.0.0.1:5000/restaurant/Dammam