-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
151 lines (117 loc) · 4.44 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from flask import Flask, render_template, redirect, url_for, request, make_response
import yfinance as yf
import pandas as pd
import datetime as dt
import os
from gunicorn import util
from config import *
import mysql.connector
db_config = {
'host': hostname,
'user': username,
'password': password,
'database': database,
}
# Function to connect to the MySQL database and fetch data from a specific column
def get_data_from_database(table_name):
connection = mysql.connector.connect(**db_config)
cursor = connection.cursor()
# Fetch all columns from the specified table
query = f"SELECT * FROM {table_name}"
cursor.execute(query)
data = cursor.fetchall()
cursor.close()
connection.close()
return data
app = Flask(__name__)
app.config['ENV'] = 'production'
@app.route('/stock_info/<symbol>')
def stock_info(symbol):
try:
stock_data = yf.Ticker(symbol)
info = stock_data.info
return render_template('stock_info.html', info=info)
except Exception as e:
return f"Error: {str(e)}"
@app.route('/test/<symbol>')
def test(symbol):
try:
stock_data = yf.Ticker(symbol)
info = {
'test'
'longName': stock_data.info.get('longName'),
'symbol': stock_data.info.get('symbol'),
'longBusinessSummary': stock_data.info.get('longBusinessSummary'),
'sector': stock_data.info.get('sector'),
'industry': stock_data.info.get('industry'),
'currentPrice': stock_data.info.get('currentPrice'),
'previousClose': stock_data.info.get('previousClose'),
'open': stock_data.info.get('open'),
'dayLow': stock_data.info.get('dayLow'),
'fullTimeEmployees': stock_data.info.get('fullTimeEmployees'),
'website': stock_data.info.get('website'),
'fiftyDayAverage':stock_data.info.get('fiftyDayAverage'),
'relto50': str(round((stock_data.info.get('currentPrice') / stock_data.info.get('fiftyDayAverage') - 1) * 100, 2)) + ' %',
'twoHundredDayAverage':stock_data.info.get('twoHundredDayAverage',)
}
return render_template('test.html', info=info)
except Exception as e:
return f"Error: {str(e)}"
# @app.route('/ai/model')
# def stock_info(symbol):
# try:
# stock_data = yf.Ticker(symbol)
# info = stock_data.info['dayLow']
# return render_template('stock_info.html', info=info)
# except Exception as e:
# return f"Error: {str(e)}"
@app.route('/home', methods=['GET', 'POST'])
def home():
return render_template('home.html')
@app.route('/')
def start():
return "Hello Abdulla !!!"
@app.route('/register')
def register():
return render_template('register.html')
@app.route('/registerV')
def registerV():
return render_template('registerV.html')
@app.route('/csv/')
def download_csv():
csv = 'foo,bar,baz\nhai,bai,crai\n'
response = make_response(csv)
cd = 'attachment; filename=mycsv.csv'
response.headers['Content-Disposition'] = cd
response.mimetype='text/csv'
return response
@app.route('/model')
def newpage():
return render_template('model.html')
# Flask route to display the data as an HTML table
@app.route('/show_data/<table_name>')
def show_data(table_name):
data = get_data_from_database(table_name)
# Connect to the database to get column names
connection = mysql.connector.connect(**db_config)
cursor = connection.cursor(dictionary=True) # Use dictionary cursor for better column name access
# Fetch column names
cursor.execute(f"SHOW COLUMNS FROM {table_name}")
column_names = [column['Field'] for column in cursor.fetchall()]
cursor.close()
connection.close()
# Render the data in an HTML table
return render_template('show_data.html', data=data, column_names=column_names, table_name=table_name)
if __name__ == '__main__':
# app.run(host='0.0.0.0', port=5000)
host = '0.0.0.0'
port = int(os.environ.get('PORT', 5000))
bind_address = f'{host}:{port}'
worker_class = 'gevent' # You can choose another worker class based on your requirements
cmd = [
'gunicorn',
'--bind', bind_address,
'--worker-class', worker_class,
'app:app',
]
os.execvp(cmd[0], cmd)