find mistakes: from flask import Flask, rendertemplatestring import pandas as pd from…
- User
find mistakes: from flask import Flask, render_template_string import pandas as pd from sqlalchemy import create_engine import datetime from utils import search_imap
Create a Flask app
app = Flask(name)
Database configuration
DATABASE_URI = 'postgresql://admin:password@10.243.167.141:5432/sms_db'
Create a database engine
engine = create_engine(DATABASE_URI)
Function to create or update DataFrame in the database
def update_database(): df = search_imap() df['Номер карты'] = df['Номер карты'].astype('string') df['Действие'] = df['Действие'].astype('string') df['Сумма'] = df['Сумма'].astype('float32') df['Время из смс'] = pd.to_datetime(df['Время из смс']) df['Время из смс'] = df['Время из смс'].dt.strftime('%H:%M') df['Время из почты'] = df['Время из почты'].astype('string')
# Write DataFrame to PostgreSQL df.to_sql('data', con=engine, if_exists='replace', index=False)@app.route('/') def display_dataframe(): # Read the DataFrame from the database query = "SELECT * FROM data" df = pd.read_sql(query, con=engine)
# Convert to HTML html_table = df.to_html(classes='table table-striped', index=False) # HTML template html_template = ''' <html> <head> <title>DataFrame Display</title> <link rel=“stylesheet” href=“https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css”> </head> <body> <div class=“container”> <h2>Displaying DataFrame</h2> {{ table|safe }} </div> </body> </html> ''' return render_template_string(html_template, table=html_table)if name == 'main': # For a real app, you might prefer a scheduling library like cron or APScheduler to run this function monthly. update_database() app.run(debug=False)
- Assistant
- User
AttributeError: 'OptionEngine' object has no attribute 'execute'
- Assistant
The error message
AttributeError: 'OptionEngine' object has no attribute 'execute'indicates that the object you're interacting with doesn't contain theexecuteattribute or method, which is likely due to thecreate_enginefunction from SQLAlchemy that's used to initialize the database connection.Here are the potential reasons and solutions:
-
Incorrect Object Reference:
- Ensure that the
engineobject you're using is indeed an instance ofsqlalchemy.engine.base.Engine, which is created withcreate_engine.
- Ensure that the
-