From d38dc2a824c544c9374247284c403ebd3dfebe36 Mon Sep 17 00:00:00 2001 From: wkyas <14069237+wkyas@user.noreply.gitee.com> Date: Thu, 20 Mar 2025 15:54:43 +0000 Subject: [PATCH] 2 Signed-off-by: wkyas <14069237+wkyas@user.noreply.gitee.com> --- .DS_Store | Bin 0 -> 6148 bytes REAMDE.md | 35 ++++++++++++ app.py | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ users.db | Bin 0 -> 12288 bytes 4 files changed, 199 insertions(+) create mode 100644 .DS_Store create mode 100644 REAMDE.md create mode 100644 app.py create mode 100644 users.db diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..2ab7c6ca1d71a64ff3a9d72135260cb60a27534f GIT binary patch literal 6148 zcmeH~L5tHs6vyA});4t!dQjMlFyOVUwOv-kOYE)(uSWEsQj=}aU^Y{l)UuR9&iY}z z`bqpc`hRa`p|#bEU>BKJ-ux#s?<JWZotX>~so`w=fT%-60i3mQ6U7$ee)bL9b1f~X zl6y?(@>y=qliVa5(Z0bG@C2?K0iL@}ifMx15qx$2@}lqre)L03UcE^rRfw-Bqw|%y z&aq$r6r0S;iCY)GC6hcWt6uNB+S_m3Xtsh@a1gwWKA3q_#nr4F#?#k)^}?7uxvIy> z%PgIbI=4?vQN>x2P9-55rwDoTDl5ijJ~XppTuN?aJA!u59(4{Ei<5`PUELq_mtDPh z)bDlm@!({+YzKGl-GBUi^fA9E%x4)PDXe8;x13(d7gW~9|0$XlxhdYGopaitlF|hz z=0Yf?a-ER6i>OtaTQYXfX~3KYf5~af$bO*(6yA19!2QpIgPp#Gf4C>$30y}4ygq0+ zYv{DLDoY0nIRZc%Xcj}AKLt2PI}M%IR?!1fHWg}9l{;c6n~rwv;zFmjRhv%A9X^zM zvT`RBB~QowwS<!jt@_dv@C4Qg?Ayx@@BdGKfBs)j@>iaKC-7epP>r+bY=|Mby>(-7 vyw_^@J2)H1)m9}1h1-s$!dvknTnv4hE6~tsZ51sr{~;h4eBlZFQvyE$=6JG7 literal 0 HcmV?d00001 diff --git a/REAMDE.md b/REAMDE.md new file mode 100644 index 0000000..ca33e37 --- /dev/null +++ b/REAMDE.md @@ -0,0 +1,35 @@ +Stock Analyzer +Project Overview +This is a Flask-based stock analysis website that offers user registration, login, stock price querying, and visualization. It leverages Flask, SQLite, Bootstrap, Matplotlib, and the Alpha Vantage API. +Directory Structure +Copy +StockAnalyzer/ +├── app.py +├── templates/ +│ ├── base.html +│ ├── index.html +│ ├── login.html +│ ├── predict_future.html +│ └── register.html +├── static/ +│ └── (images, CSS, JS files) +└── README.md +Installation and Launch +1. Install Dependencies +Ensure you have Python and pip installed. Then, install the required packages: +bashCopy +pip install flask pandas matplotlib requests sqlite3 +2. Get Alpha Vantage API Key +Sign up at Alpha Vantage to get a free API key. Replace 'YOUR_API_KEY' in the get_stock_data_alpha_vantage function in app.py with your key. +3. Start the Flask App +Run the following command in the project root directory to start the Flask app: +bashCopy +python app.py +The app will run locally, accessible at http://127.0.0.1:50582. +4. Access the Website +Open your browser and navigate to http://127.0.0.1:50582 to see the homepage. You can register, log in, query stock prices, and view visualizations. +Features +User registration and login. +Stock price querying and visualization. +Responsive design using Bootstrap. +Immediate user feedback with Flask's flash function. diff --git a/app.py b/app.py new file mode 100644 index 0000000..1af3aab --- /dev/null +++ b/app.py @@ -0,0 +1,164 @@ +import requests +import pandas as pd +import base64 +from flask import Flask, render_template, request, redirect, url_for, flash, session, send_file +import matplotlib +import matplotlib.pyplot as plt +from io import BytesIO +import sqlite3 +matplotlib.use("Agg") + +app = Flask(__name__) +app.secret_key = 'secret_key' + +# init database +def init_db(): + conn = sqlite3.connect('users.db') + c = conn.cursor() + c.execute('''CREATE TABLE IF NOT EXISTS users (username TEXT PRIMARY KEY, password TEXT)''') + conn.commit() + conn.close() + +init_db() + +# connect database +def get_db_connection(): + conn = sqlite3.connect('users.db') + conn.row_factory = sqlite3.Row + return conn + +# sign up +@app.route('/register', methods=['GET', 'POST']) +def register(): + if request.method == 'POST': + username = request.form['username'] + password = request.form['password'] + + conn = get_db_connection() + c = conn.cursor() + + # check whether the user exist + c.execute('SELECT * FROM users WHERE username = ?', (username,)) + user = c.fetchone() + + if user: + flash('Username already exists. Please choose a different one.', 'danger') + conn.close() + return redirect(url_for('register')) + + # save into database + c.execute('INSERT INTO users (username, password) VALUES (?, ?)', (username, password)) + conn.commit() + conn.close() + + flash('Registration successful! You can now log in.', 'success') + return redirect(url_for('login')) + + return render_template('register.html') + +# log in +@app.route('/login', methods=['GET', 'POST']) +def login(): + if request.method == 'POST': + username = request.form['username'] + password = request.form['password'] + + conn = get_db_connection() + c = conn.cursor() + + # check user and password + c.execute('SELECT * FROM users WHERE username = ? AND password = ?', (username, password)) + user = c.fetchone() + + conn.close() + + if user: + session['username'] = username + flash('Login successful!', 'success') + return redirect(url_for('index')) + else: + flash('Invalid username or password. Please try again.', 'danger') + + return render_template('login.html') + +# log out +@app.route('/logout') +def logout(): + session.pop('username', None) + flash('You have been logged out.', 'info') + return redirect(url_for('login')) + +# main page +@app.route('/') +def index(): + if 'username' not in session: + flash('Please log in to access the dashboard.', 'warning') + return redirect(url_for('login')) + return render_template('index.html', username=session['username']) + +# plot stock +@app.route('/predict_future', methods=['GET', 'POST']) +def predict_future(): + if 'username' not in session: + flash('Please log in to access this feature.', 'warning') + return redirect(url_for('login')) + + img_base64 = None + stock_symbol = None + start_date = None + end_date = None + + if request.method == 'POST': + stock_symbol = request.form['stock_symbol'] + start_date = request.form['start_date'] + end_date = request.form['end_date'] + + # get stock price data + try: + stock_data = get_stock_data_alpha_vantage(stock_symbol, start_date, end_date, 'VLP8G4UP0NW3RFAA') + print(f"Stock data for {stock_symbol} from {start_date} to {end_date}:") + print(stock_data) + except Exception as e: + flash(f'Error fetching stock data: {e}', 'danger') + return render_template('predict_future.html', stock_symbol=stock_symbol, start_date=start_date, end_date=end_date) + + if stock_data.empty: + flash(f'No data available for {stock_symbol} from {start_date} to {end_date}.', 'warning') + return render_template('predict_future.html', stock_symbol=stock_symbol, start_date=start_date, end_date=end_date) + + # plot + plt.figure(figsize=(10, 5)) + plt.plot(stock_data.index.to_numpy(), stock_data['4. close'].to_numpy(), label='Close Price') + plt.title(f'Stock Price for {stock_symbol}') + plt.xlabel('Date') + plt.ylabel('Price') + plt.legend() + plt.grid(True) + + img = BytesIO() + plt.savefig(img, format='png') + img.seek(0) + img_base64 = base64.b64encode(img.getvalue()).decode('utf8') + plt.close() + + return render_template('predict_future.html', img_base64=img_base64, stock_symbol=stock_symbol, start_date=start_date, end_date=end_date) + + +def get_stock_data_alpha_vantage(symbol, start_date, end_date, api_key): + url = f'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={symbol}&apikey={api_key}&outputsize=full' + r = requests.get(url) + data = r.json() + + # decode data + time_series = data.get('Time Series (Daily)', {}) + df = pd.DataFrame.from_dict(time_series, orient='index', dtype=float) + df.index = pd.to_datetime(df.index) + df = df.sort_index() + + # set date + df = df[(df.index >= start_date) & (df.index <= end_date)] + + return df + +if __name__ == '__main__': + app.run(debug=True, port=50582) \ No newline at end of file diff --git a/users.db b/users.db new file mode 100644 index 0000000000000000000000000000000000000000..4fb527ee87a05b18caf81ff464a9f96d83322865 GIT binary patch literal 12288 zcmeI$F-yZh6bJBkcVeqjuiGJAZnPjmthxzC$sktLn88jV(h~}8qD@L%9Q=TO1;2oc zn~vU{**jPM5AMBtcO;PC_HuVSoK!|X7V}wF=>c_#F*?<nh(xn&@xg3fYxb_Vepv|G zzkcn=AL6nmS<Clj58xgG5P$##AOHafKmY;|fB*y_@Q(x%)>2Bbw^o%sOwFP+^U^1N z9!Fsk=_EWGM%o|gU03pKW^@wWC;BG7xC-NuzKlkD`Y9{Rx|omsRlm>I)rn#>$;alU ze4g6LKFt<Y;rrCTEe(8evwhoncI@KvgXEV@2nav`0uX=z1Rwwb2tWV=5P$##R#2eJ zMQ_Iq{6%hpgTdj^@ir66p9FvXru`}|KS_Sugn$49AOHafKmY;|fB*y_009U<VATb- QM2|K90(e`f4Yv<|0S@a%h5!Hn literal 0 HcmV?d00001 -- GitLab