我正在使用JSON
和request
库获取ExchangeRates API的JSON,使用Pandas读取SCV并转换表,在前端页面中呈现HTML,使用Forex转换速率
我注意到页面仅在45秒后加载,我认为问题在于CSV文件和to_html()
的转换。我有6张巴西的桌子,5张新西兰的桌子和7张乌拉圭的桌子,总共18张桌子
有没有办法加快CSV和表格的速度
from django.shortcuts import get_object_or_404, render
from django.views import View
from forex_python.converter import CurrencyRates
from pathlib import Path
import json
import requests
from pandas.io.parsers import read_csv
class financial_planning(Mixin, View):
def get(self, request, id = None, *args, **kwargs):
template = "pages/financial-planning.html"
context = {
'title': "Planejamentos financeiros",
'brazil_bills': self.brazil_bills(),
'nz_bills': self.nz_bills(),
'uy_bills': self.uy_bills(),
}
return render(request, template, context)
def uruguayan_currency_conversion(self):
url = 'https://v6.exchangerate-api.com/v6/YYYYEEESSS/latest/UYU'
response = requests.get(url)
data = response.json()
return data
def brazil_bills(self):
cc = CurrencyRates()
cad = cc.convert('BRL', 'CAD', 1)
nzd = cc.convert('BRL', 'NZD', 1)
usd = cc.convert('BRL', 'USD', 1)
base_path = Path(__file__).parent
file_path = (base_path / "static/data/brazil/bills.csv").resolve()
c = pd.read_csv(file_path)
c.loc["Total"] = c.sum()
c["Item"].values[-1] = "Total"
c["USD"] = (c['Price (BRL)'] * usd).round().astype(int)
c["CAD"] = (c['Price (BRL)'] * cad).round().astype(int)
c["NZD"] = (c['Price (BRL)'] * nzd).round().astype(int)
html_table = c.to_html(classes = 'bills', index = False)
return html_table
def nz_bills(self):
cc = CurrencyRates()
brl = cc.convert('NZD', 'BRL', 1)
cad = cc.convert('NZD', 'CAD', 1)
usd = cc.convert('NZD', 'USD', 1)
base_path = Path(__file__).parent
file_path = (base_path / "static/data/nz/bills.csv").resolve()
c = pd.read_csv(file_path)
c.loc["Total"] = c.sum()
c["Item"].values[-1] = "Total"
c["BRL"] = (c['Price (NZD)'] * brl).round().astype(int)
c["CAD"] = (c['Price (NZD)'] * cad).round().astype(int)
c["USD"] = (c['Price (NZD)'] * usd).round().astype(int)
html_table = c.to_html(classes = 'bills', index = False)
return html_table
def uy_bills(self):
data = self.uruguayan_currency_conversion()
base_path = Path(__file__).parent
file_path = (base_path / "static/data/uruguay/bills.csv").resolve()
c = pd.read_csv(file_path)
c.loc["Total"] = c.sum()
c["Item"].values[-1] = "Total"
c["BRL"] = (data['conversion_rates']['BRL'] * c['Price (UYU)']).round().astype(int)
c["CAD"] = (data['conversion_rates']['CAD'] * c['Price (UYU)']).round().astype(int)
c["NZD"] = (data['conversion_rates']['NZD'] * c['Price (UYU)']).round().astype(int)
c["USD"] = (data['conversion_rates']['USD'] * c['Price (UYU)']).round().astype(int)
html_table = c.to_html(classes = 'bills', index = False)
return html_table
如果要查看完整的views.py
,可以选中here
目前没有回答
相关问题 更多 >
编程相关推荐