简体   繁体   English

Flask/Python - 我如何在 Flask 中运行这个 python 脚本并在 ZFC35FDC70D5FC69D256987A 页面中渲染 output

[英]Flask/Python - How can I run this python script in Flask and render output in html page

Im writting this code to get some values of stock prices, and how can I render output results in results.html page?我编写此代码以获取一些股票价格值,以及如何在 results.html 页面中呈现 output 结果? I'm using Heroku to make this app.我正在使用 Heroku 来制作这个应用程序。

Please help me with this app.请帮助我使用这个应用程序。 This is the main file to call others files.这是调用其他文件的主文件。

Please tell me if need more details to run this app.如果需要更多详细信息来运行此应用程序,请告诉我。

Here are the codes I've made, to display the output in view.html page.这是我制作的代码,用于在 view.html 页面中显示 output。 x.py x.py

print('----EURO DO REMESSA ONLINE-----')
import melhor
print('-----COTAÇÃO DOLARHOJE.COM-----')
import cota
print('-----INDICADOR EURO COMERCIAL H1----')
import indicator
print('-----INVESTING VALOR EURO COM-----')
import invest
print( '-------------\n', 'Conhece a ti mesmo. (Tales de Mileto)', '\n-------------')

melhor.py melhor.py

import requests
from bs4 import BeautifulSoup as bs
import time
import re
from Color import *
url = 'https://www.remessaonline.com.br/cotacao/cotacao-euro'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
page = requests.get(url, headers=headers)
page
page.status_code
page.text
soup = bs(page.text, 'html.parser')
resumo = soup.find_all('div', class_="style__Text-sc-27fg4f-2 ddwOcG")[0].get_text('')
mult = float(re.sub('[^0-9.]', "", resumo.replace(",",".")))
print('Euro COMERCIAL: R$', Base.BOLD, ANSI_Compatible.Color(120), mult, ANSI_Compatible.END, Base.END)
eur_rate = 1.035145
eur_ra = str(mult * eur_rate)[:6]
print('Euro TURISMO: R$', Base.BOLD, eur_ra, Base.END)
current = time.strftime("%Y / dia:%d mês:%m horário:%H:%M:%S")
print(current)
with open('remessa.txt', 'a') as f:
    f.writelines('Valor euro turismo:' + 'R$' + eur_ra + ' ' + 'Data:' + current + '\n')

cota.py副本

import requests
import time

response = requests.get('http://dolarhoje.com/cotacao.txt')
data = response.text
print('1 USD comercial =',"R$",data )

response = requests.get('http://dolarhoje.com/dolar-turismo/cotacao.txt')
data = response.text
print('1 USD turismo =',"R$",data )

response = requests.get('http://dolarhoje.com/euro/cotacao.txt')
data2 = response.text
print('1 EUR comercial =',"R$",data2 )

response = requests.get('http://dolarhoje.com/euro-turismo/cotacao.txt')
data2 = response.text
print('1 EUR turismo =',"R$",data2 )

response = requests.get('http://dolarhoje.com/bitcoin-hoje/cotacao.txt')
data3 = response.text
print('1 BTC =',"R$",data3 )


current = time.strftime("ano:%Y dia:%d mês:%m horário:%H:%M:%S")
with open('cota.txt', 'a') as f:
    f.writelines('Valor euro turismo:' + 'R$' + data2 + ' ' + 'Data:' + current + '\n')

indicator.py指标.py

import requests
from bs4 import BeautifulSoup as bs


url = 'https://br.investing.com/currencies/eur-brl-technical'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
page = requests.get(url, headers=headers)
page
page.status_code
page.text
soup = bs(page.text, 'html.parser')
resumo = soup.find_all(id='techStudiesInnerWrap')[0].get_text(' ')
print(resumo) 

invest.py投资.py

import requests
from bs4 import BeautifulSoup as bs


url = 'https://br.investing.com/currencies/eur-brl-technical'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
page = requests.get(url, headers=headers)
page
page.status_code
page.text
soup = bs(page.text, 'html.parser')
resumo = soup.find_all('div', class_="top bold inlineblock")[0].get_text(' ')
print(resumo) 

Thanks for your attention.感谢您的关注。

I hope I can be helpful, and I don't know much about Python.希望对大家有所帮助,对Python了解不多。 But it sounds like if you're doing Server-side rendering of the html page, you just need a template engine such as Jade, Pug, or something of that sort.但听起来如果您正在对 html 页面进行服务器端渲染,您只需要一个模板引擎,例如 Jade、Pug 或类似的东西。

Here are some examples for you to follow:以下是一些示例供您参考:

https://www.grizzlypeaksoftware.com/articles?id=1m3W3llyJLy4YS5OpyyxBq https://www.grizzlypeaksoftware.com/articles?id=1m3W3llyJLy4YS5OpyyxBq

https://scotch.io/tutorials/getting-started-with-flask-a-python-microframework https://scotch.io/tutorials/getting-started-with-flask-a-python-microframework

https://scoutapm.com/blog/python-flask-tutorial-getting-started-with-flask https://scoutapm.com/blog/python-flask-tutorial-getting-started-with-flask

https://www.liquidweb.com/kb/getting-started-with-flask/ https://www.liquidweb.com/kb/getting-started-with-flask/

https://github.com/akibrhast/flask_base_template https://github.com/akibrhast/flask_base_template

Basically what you need to learn about is how to create a backend server.基本上你需要了解的是如何创建一个后端服务器。 In this case using flask.在这种情况下使用 flask。 The templating engine that is typically used with flask is called Jinja.通常与 flask 一起使用的模板引擎称为 Jinja。 Once you make your request call to your data you would want to pass it through to your routes via jinja.一旦您对您的数据发出请求调用,您就会希望通过 jinja 将其传递给您的路线。

Here is a basic example:这是一个基本示例:

from flask import Flask, flash, redirect, render_template, request, session, abort,url_for
import os

#from flask_login import login_user, current_user, logout_user, login_required #Optional login imports, need to 'pip install flask_login'

app = Flask(__name__)

@app.route('/')
def index():
  return render_template("base.html")

if __name__ == '__main__':
    app.secret_key = os.urandom(12)
    app.run(debug=True)

I solved the problem: Here is the solution I've found:我解决了这个问题:这是我找到的解决方案:

First, you can visit the app here: finance-python.herokuapp.com首先,您可以在这里访问应用程序: finance-python.herokuapp.com

app.py应用程序.py

from flask import Flask
from functools import wraps
import sqlite3
import requests
import bs4
import requests_html
from flask import render_template
import subprocess
import re
import flask
from bs4 import BeautifulSoup as bs
import time

app = flask.Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def index():
    url = 'https://www.remessaonline.com.br/cotacao/cotacao-euro'
    headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
    page = requests.get(url, headers=headers)
    page
    page.status_code
    page.text
    soup = bs(page.text, 'html.parser')
    resumo = soup.find_all('div', class_="style__Text-sc-27fg4f-2 ddwOcG")[0].get_text('')
    mult = float(re.sub('[^0-9.]', "", resumo.replace(",",".")))
    euro_comercial_rem = print('Euro COMERCIAL: R$', mult)
    eur_rate = 1.035145
    eur_ra = str(mult * eur_rate)[:6]
    euro_turismo_rem = print('Euro TURISMO: R$', eur_ra)
    current = time.strftime("%Y / dia:%d mês:%m horário:%H:%M:%S")
    print(current)

    response = requests.get('http://dolarhoje.com/cotacao.txt')
    data = response.text
    dolar_com_dolar_hoje = print('1 USD comercial =',"R$",data )
    response = requests.get('http://dolarhoje.com/dolar-turismo/cotacao.txt')
    dataa = response.text
    dolar_tur_dolar_hoje = print('1 USD turismo =',"R$",dataa )

    response = requests.get('http://dolarhoje.com/euro/cotacao.txt')
    data2 = response.text
    euro_com_dolar_hoje = print('1 EUR comercial =',"R$",data2 )
    response = requests.get('http://dolarhoje.com/euro-turismo/cotacao.txt')
    dataa2 = response.text
    euro_tur_dolar_hoje = print('1 EUR turismo =',"R$",dataa2 )

    response = requests.get('http://dolarhoje.com/bitcoin-hoje/cotacao.txt')
    data3 = response.text
    btc_dolar_hoje = print('1 BTC =',"R$",data3 )


    current = time.strftime("ano:%Y dia:%d mês:%m horário:%H:%M:%S")

    url = 'https://br.investing.com/currencies/eur-brl-technical'
    headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
    page = requests.get(url, headers=headers)
    page
    page.status_code
    page.text
    soup = bs(page.text, 'html.parser')
    resumoo = soup.find_all(id='techStudiesInnerWrap')[0].get_text(' ')
    resumo_inves = print(resumoo)

    url = 'https://br.investing.com/currencies/eur-brl-technical'
    headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
    page = requests.get(url, headers=headers)
    page
    page.status_code
    page.text
    soup = bs(page.text, 'html.parser')
    resumooo = soup.find_all('div', class_="top bold inlineblock")[0].get_text(' ')
    resumo_invest_eur_brl = print(resumooo)
    #resumos = 'eur_ra' + 'euro_turismo_rem' + 'dolar_com_dolar_hoje' + 'dolar_tur_dolar_hoje' + 'btc_dolar_hoje' + 'resumo_inves' + 'euro_com_dolar_hoje' + 'euro_tur_dolar_hoje'

    return flask.render_template('index.html', mult=mult, eur_ra=eur_ra, current=current, data=data, dataa=dataa, data2=data2, dataa2=dataa2, data3=data3, resumoo=resumoo, resumooo=resumooo)

if __name__ == '__main__':
    app.run()

index.html into template folder index.html 进入模板文件夹

-----EURO DO REMESSA ONLINE----- <br>
EURO COMERCIAL : R$ {{ mult }} <br>
EURO TURISMO: R$ {{ eur_ra }} <br>
{{ current }} <br> <br>
----COTAÇÃO DOLARHOJE.COM----- <br>
1 USD comercial = R$ {{ data }} <br>
1 USD turismo = R$ {{ dataa }} <br>
1 EUR comercial = R$ {{ data2 }} <br>
1 EUR turismo = R$ {{ dataa2 }} <br>
1 BTC = R$ {{ data3 }} <br> <br>
-----INDICADOR EURO COMERCIAL H1----- <br>
{{ resumoo }} <br> <br>
-----VARIAÇÃO DO EURO-----<br>
{{ resumooo }} <br> <br>

Conhece a ti mesmo - Tales de Mileto

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM