【发布时间】:2022-01-14 13:47:36
【问题描述】:
我正在尝试为烧瓶中的生产和开发环境配置我的 postgresql。现在我的配置仅适用于本地环境。我也想让它适用于生产。这就是它的样子:
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://username:password@localhost:5432/database"
现在这个项目中完成的所有工作,包括数据库配置都包含在 app.py 文件中。我还有一个索引函数,我每 5 分钟运行一次。间隔值来自数据库。这是我的 app.py 文件.
from flask import Flask, render_template, Response, json
from flask_sqlalchemy import SQLAlchemy
from bs4 import BeautifulSoup
import requests
from sqlalchemy import text
import uuid
from stockDto import StockDto
import schedule
import time
stockDtoList = StockDto(many=True)
app = Flask(__name__)
db = SQLAlchemy(app)
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://username:password@localhost:5432/database"
def generate_uuid():
return str(uuid.uuid4())
class StockStatus(db.Model):
__tablename__ = 'stockstatus'
id = db.Column(db.String, primary_key=True, default=generate_uuid)
name = db.Column(db.Text)
ltp = db.Column(db.Text)
high = db.Column(db.Text)
low = db.Column(db.Text)
@app.route('/stockList', methods=['GET'])
def getStock():
try:
articles = StockStatus.query.all()
resultList = stockDtoList.dump(articles)
return Response(json.dumps({'status': 'success', 'message': 'data Found', 'data': resultList}),
status=200, mimetype='application/json')
except Exception as e:
print('exception is :: ', e)
return Response(json.dumps({'status': 'failed', 'message': 'data failed to get'}),
status=500, mimetype='application/json')
# @app.route('/')
def index():
connection = db.engine.connect(close_with_result=True)
print('first')
sql = text("""delete from stockstatus""")
print('done')
connection.execute(sql)
connection.close()
r = requests.get("http://www.dsebd.org/latest_share_price_scroll_l.php")
# Create a BeautifulSoup object
soup = BeautifulSoup(r.content, 'html5lib')
soup.prettify()
table = soup.find('table', attrs={
'class': 'table table-bordered background-white shares-table fixedHeader'})
quotes = [] # a list to store quotes
for row in table.find_all('tr')[1:]:
cols = row.find_all('td')
quotes.append({'name': cols[1].text.strip().replace(",", ""),
'ltp': cols[2].text.strip().replace(",", ""),
'high': cols[3].text.strip().replace(",", ""),
'low': cols[4].text.strip().replace(",", ""),
})
for SidesValue in quotes:
dataMedia = StockStatus(
name=SidesValue['name'],
ltp=SidesValue['ltp'],
high=SidesValue['high'],
low=SidesValue['low'],
)
db.session.add(dataMedia)
db.session.commit()
articles = StockStatus.query.all()
# return render_template("index.html", articles=articles)
connection = db.engine.connect(close_with_result=True)
sqlUpdate = text("""select schedulevalue from stocksettings""")
scheduleStatus = connection.execute(sqlUpdate).fetchone()
dataInterval = int(scheduleStatus['schedulevalue'])
print(dataInterval)
schedule.every(dataInterval).minutes.do(index)
# scheduler wait for 5 mins
while True:
schedule.run_pending()
time.sleep(dataInterval)
if __name__ == "__main__":
app.run()
我可以通过更好的文件夹结构安排以更好的方式组织这个项目。此外,在运行调度程序的项目上也应该工作。任何关于此的建议将不胜感激
【问题讨论】:
标签: python postgresql flask sqlalchemy production-environment