ljluestc commented on issue #28148:
URL: https://github.com/apache/superset/issues/28148#issuecomment-2726190195
```
import pandas as pd
from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine
# Initialize Flask app
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///pivot_data.db' # Use
SQLite for simplicity
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# Define a model for the pivot table data
class PivotData(db.Model):
__tablename__ = 'pivot_data'
id = db.Column(db.Integer, primary_key=True)
series = db.Column(db.String(50)) # e.g., "Puzzle"
metric = db.Column(db.String(50)) # e.g., "Sales", "Profit"
value = db.Column(db.Float) # Numeric value
# Simulate your data (replace with your actual data source)
def create_sample_data():
data = {
'series': ['Puzzle', 'Puzzle', 'Action', 'Action'],
'metric': ['Sales', 'Profit', 'Sales', 'Profit'],
'value': [100, 20, 150, 30]
}
df = pd.DataFrame(data)
# Sort by series first, then value descending within series
df = df.sort_values(by=['series', 'value'], ascending=[True, False])
return df
# Populate the database
def init_db():
db.drop_all()
db.create_all()
df = create_sample_data()
for _, row in df.iterrows():
entry = PivotData(series=row['series'], metric=row['metric'],
value=row['value'])
db.session.add(entry)
db.session.commit()
# Flask route to serve data (for debugging)
@app.route('/data', methods=['GET'])
def get_data():
data = PivotData.query.all()
result = [{'series': d.series, 'metric': d.metric, 'value': d.value} for
d in data]
return jsonify(result)
if __name__ == '__main__':
with app.app_context():
init_db() # Initialize database with sorted data
app.run(debug=True, host='0.0.0.0', port=5000)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]