Issue
I am creating a website in html and flask to which you can upload an xlsx file. I need to do this so that I can view the sheets of this file. I have already created a file addition and output the names of its sheets to the datalist. Now I need to select the desired sheet from the list to display it on the same page. How can this be implemented?(preferably in python, or together with js. in extreme cases, only on js.) Here is my python code.
from flask import Flask, render_template, request, jsonify
from fileinput import filename
import pandas as pd
app = Flask(__name__)
@app.route('/initialization', methods=['GET','POST'])
def readfile():
file = request.files['file']
file.save(file.filename)
data = pd.ExcelFile(file)
return render_template('readfile.html',sheets = data.sheet_names)
if __name__ == '__main__':
app.run(debug=True)
HTML:
<input type="text" name="sheet" list="sheet_list" placeholder="Choose sheet">
<datalist id="sheet_list">
{% for sheet in sheets %}
<option>{{sheet}}</option>
{% endfor %}
</datalist>
Solution
If I understand your question correctly you are trying to read an uploaded Excel file, have the user choose a sheet, and then display it's contents.
You may be able to do this with minimal modification I believe, where you can select a sheet from the list and display its contents on a new or same page.
First, Update your HTML form to include a submit button:
<form action="/display_sheet" method="post">
<input type="text" name="sheet" list="sheet_list" placeholder="Choose sheet">
<datalist id="sheet_list">
{% for sheet in sheets %}
<option>{{sheet}}</option>
{% endfor %}
</datalist>
<input type="submit" value="Display Sheet">
</form>
Second, create a new route in your Flask application to handle the sheet display:
@app.route('/display_sheet', methods=['POST'])
def display_sheet():
selected_sheet = request.form['sheet'] # Get the selected sheet from the form
file = request.files['file']
file.save(file.filename)
data = pd.read_excel(file, sheet_name=selected_sheet) # Read the selected sheet
# Pass the data to the template for rendering
return render_template('display_sheet.html', sheet_name=selected_sheet, sheet_data=data.to_html())
Lastly, create a new template for displaying the sheet, for example, display_sheet.html
:
<!DOCTYPE html>
<html>
<head>
<title>Display Sheet</title>
</head>
<body>
<h2>Sheet: {{ sheet_name }}</h2>
{{ sheet_data | safe }}
</body>
</html>
In this template, we use {{ sheet_name }}
to display the name of the selected sheet, and {{ sheet_data | safe }}
to display the HTML table representation of the sheet data. Using | safe
is important to avoid HTML escaping.
With these changes, when you submit the form by selecting a sheet, it will call the /display_sheet
route, which will read the selected sheet from the uploaded Excel file and display it on a new page.
You can always adjust the template and style to your requirements.
Additionally, you might want to add error handling to avoid cases where the selected sheet does not exist in the Excel file.
Answered By - neelrast
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.