我写了使用两个API来获取有关当前的密码趋势数据的PY文件。我试图导入或致电检索到的数据在一个HTML文件做显示在我的web应用程序。
我一直在使用{{%}}调用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文件中把这个数据,因此它可以在Web应用程序中显示。
我给你的东西入手;把它放在一个类,它返回填充了数据的字典(也可以是一个普通的函数)
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
然后在视图:
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>
与这一问题,任何时候有人触及的网页,你会自动发送一个GET请求到coingecko。这可以是速率限制的问题
所以你可以做你的实例在CryptoData但views.py您的视图功能之外。
所以更好的实现这可能限制了数据更新为每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%
这真的取决于你有Web应用程序。
做到这一点最简单的方法是Griehle建议,用瓶。这是一个非常简单的框架。
我也建议您在功能移动这个代码,并遍历结果,你实际上得到,而不是反复的硬编码数的数量。