gpt4 book ai didi

python - 如何在django框架内导入或调用HTML中的PY代码

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:45 24 4
gpt4 key购买 nike

我编写了一个 py 文件,它使用两个 API 来检索有关当前加密货币趋势的数据。我正在尝试导入或调用在我的网络应用程序中显示的 HTML 文件中检索到的数据。

我尝试使用 {{% %}} 调用 py 文件,但不确定我是否做对了。

import requests

url_usd = 'https://api.coingecko.com/api/v3/coins/markets?
vs_currency=usd&order=market_cap_desc&per_page=250&page=1' \
'&sparkline=false&price_change_percentage=24h'

url_gbp = 'https://api.coingecko.com/api/v3/coins/markets?
vs_currency=gbp&order=market_cap_desc&per_page=250&page=1' \
'&sparkline=false&price_change_percentage=24h '

requests1 = requests.get(url_usd)
results1 = requests1.json()

requests2 = requests.get(url_gbp)
results2 = requests2.json()

for i in range(0, 250):
coin_id = results1[i]['id']
coin_name = results1[i]['name']
changes = results1[i]['price_change_percentage_24h']
usd = results1[i]['current_price']
gbp = results2[i]['current_price']

print("Coin ID: " + coin_id)
print("Coin name: " + coin_name)
print("Price per coin in USD: " + "$" + "{:.2f}".format(float(usd)))
print("Price per coin in GBP: " + "£" + "{:.2f}".format(float(gbp)))
print("Percentage price change: " + "{:.2f}".format(changes) + "%")
print()

输出:

Coin ID: bitcoin
Coin name: Bitcoin
Price per coin in USD: $3461.54
Price per coin in GBP: £2645.04
Percentage price change: 0.82%

Coin ID: ripple
Coin name: XRP
Price per coin in USD: $0.31
Price per coin in GBP: £0.23
Percentage price change: -0.60%

接下来的 250 个硬币依此类推

我现在想从 html 文件调用此数据,以便它可以显示在网络应用程序上。

最佳答案

我会给你一些开始的东西;将其放入一个类中,该类返回一个填充有数据的字典(也可以是普通函数)

import requests

class CryptoData:
def __init__(self):
self.usd_url = "https://api.coingecko.com/api/v3/coins/markets?" \
"vs_currency=usd&order=market_cap_desc&per_page=250&page=1" \
"&sparkline=false&price_change_percentage=24h"
self.gbp_url = "https://api.coingecko.com/api/v3/coins/markets?" \
"vs_currency=gbp&order=market_cap_desc&per_page=250&page=1" \
"&sparkline=false&price_change_percentage=24h"


def get_crypto_data_dict(self, get_it=False):
crypto_dict = dict()
requests1 = requests.get(self.usd_url)
results1 = requests1.json()

requests2 = requests.get(self.gbp_url)
results2 = requests2.json()



for i in range(0, 250):
crypto_dict[results1[i]['id']] = {
'coin_name': results1[i]['name'],
'changes': results1[i]['price_change_percentage_24h'],
'usd': results1[i]['current_price'],
'gbp': results2[i]['current_price']
}


return crypto_dict

然后在 View 中:

def crypt_view(request):
crypto_data = CryptoData()

context = {
'crypto_data': crypto_data.get_crypto_data_dict()
}

return render(request, 'crypto/crypto.html', context=context)

然后在 crypto.html 中(这只是一个例子):

<ul>
{% for crypto_datum, crypto_value in crypto_data.items %}
<li>{{ crypto_datum }}
<ul>

{% for info, value in crypto_value.items %}
<li>{{ info }}: {{ value }}</li>
{% endfor %}


</ul>

</li>
{% endfor %}

</ul>

有一个问题,每当有人触摸该网页时,您都会自动向 coingecko 发送一个 get 请求。这可能是速率限制的问题

所以你可以在views.py中实例化你的CryptoData,但在你的 View 函数之外。

因此,可以将数据更新限制为每 60 秒的更好实现是这样的:

import requests, time
from django.shortcuts import render

class CryptoData:
def __init__(self):
self.usd_url = "https://api.coingecko.com/api/v3/coins/markets?" \
"vs_currency=usd&order=market_cap_desc&per_page=250&page=1" \
"&sparkline=false&price_change_percentage=24h"
self.gbp_url = "https://api.coingecko.com/api/v3/coins/markets?" \
"vs_currency=gbp&order=market_cap_desc&per_page=250&page=1" \
"&sparkline=false&price_change_percentage=24h"

self.previous_request = None
self.crypto_dict = dict()


def get_crypto_data_dict(self, seconds_to_wait=60):

if not self.previous_request or self.previous_request+seconds_to_wait < time.time():
print("requested", self.previous_request, time.time())
self.previous_request = time.time()
crypto_dict = dict()
requests1 = requests.get(self.usd_url)
results1 = requests1.json()

requests2 = requests.get(self.gbp_url)
results2 = requests2.json()

for i in range(0, 250):
self.crypto_dict[results1[i]['id']] = {
'coin_name': results1[i]['name'],
'changes': results1[i]['price_change_percentage_24h'],
'usd': results1[i]['current_price'],
'gbp': results2[i]['current_price']
}

return self.crypto_dict


crypto_data = CryptoData()

def crypt_view(request):

context = {
'crypto_data': crypto_data.get_crypto_data_dict()
}

return render(request, 'crypto/crypto.html', context=context)

通过此实现,coingecko api 仅每 60 秒调用一次

编辑:以与您可以执行类似操作相同的方式显示它:

{% for crypto_datum, crypto_values in crypto_data.items %}
<div>
<p>Coin ID: {{ crypto_datum }}<br>
Coin Name: {{ crypto_datum|capfirst }}<br>
Price per coin in USD: ${{ crypto_values.usd|floatformat:2 }}<br>
Price Per coin in GBP: £{{ crypto_values.gbp|floatformat:2 }}<br>
Percentage price change: {{ crypto_values.changes|floatformat:2 }}%
</p>
</div>
{% endfor %}

这看起来像这样:

Coin ID: bitcoin
Coin Name: Bitcoin
Price per coin in USD: $3466.24
Price Per coin in GBP: £2657.72
Percentage price change: 0.16%

Coin ID: ripple
Coin Name: Ripple
Price per coin in USD: $0.30
Price Per coin in GBP: £0.23
Percentage price change: -0.85%

Coin ID: ethereum
Coin Name: Ethereum
Price per coin in USD: $107.27
Price Per coin in GBP: £82.25
Percentage price change: -0.11%

关于python - 如何在django框架内导入或调用HTML中的PY代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54484983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com