精易论坛

标题: fastapi学习中,边学边记录 [打印本页]

作者: realpc    时间: 2023-12-16 14:29
标题: fastapi学习中,边学边记录
本帖最后由 realpc 于 2023-12-16 16:46 编辑

[Python] 纯文本查看 复制代码
from fastapi import FastAPI
import requests

app = FastAPI()

def get_weather_forecast(city: str):
    # 定义api_key
    api_key = "your_api_key"
    # 定义base_url
    base_url = "http://api.openweathermap.org/data/2.5/forecast?q={}&appid={}"
    # 拼接url
    complete_url = base_url.format(city, api_key)
    # 发送请求
    response = requests.get(complete_url)
    # 获取响应数据
    data = response.json()
    # 返回数据
    return data["list"]

@app.get("/weather/{city}")
async def weather_forecast(city: str):
    # 调用get_weather_forecast函数
    forecast = get_weather_forecast(city)
    # 返回数据
    return {"city": city, "forecast": forecast}
@app.get("/api")
async def api():
    # 定义对象数据
    object_data = {
        "提示": "彩色代码:初检通过,黑色:初检错误,**:初检有多发性错误。",
        "数值型": 123.45,
        "文本型": "一段文本",
        "逻辑型": True,
        "空": None,
        "数组": ["数组成员1", 2015],
        "未知类型": "127.0.0.1"
    }
    # 定义错误示范数据
    error_demo_data = {
        "一般错误": "文本不用引号"
    }
    # 定义结果
    result = {
        "对象": object_data,
        "错误示范": error_demo_data
    }
    # 返回结果
    return resultfrom fastapi import FastAPI


作者: 123456ppt    时间: 2023-12-16 16:17
openweathermap是什么呀                  
作者: 羊了个羊    时间: 2023-12-16 17:30
FastAPI 好像只能用来做接口,最近也在学习这块




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4