Skip to content

Added read write data with excel using flask in pandas #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from flask import *
import pandas as pd
import os
import re
app = Flask(__name__)

@app.route("/")
def show_tables():
filename = 'example2.xlsx'
data = pd.read_excel(filename,sheetname='Sheet1')
data = data.fillna('')
return render_template('index.html',tables=[re.sub(' mytable', '" id="example', data.to_html(classes='mytable'))],
titles = ['Excel Data to Flask'])



@app.route('/insert', methods= ['POST','GET'])
def insert():
q1 = request.form['num1']
q2 = request.form['num2']
print(q1,q2)
df = pd.DataFrame({'a': [q1],
'b': [q2]})

book = pd.read_excel('example2.xlsx')
writer = pd.ExcelWriter('example2.xlsx', engine='openpyxl')
book.to_excel(writer, startrow=0, index=False)
df.to_excel(writer, startrow=len(book) + 1, header=False, index=False)
writer.save()
return redirect('/')

@app.route('/save', methods= ['POST','GET'])
def save():
url = 'http://127.0.0.1:5000/'
urll = request.get_data()
print(urll)
data = pd.read_html(urll)
print(data)
writer = pd.ExcelWriter('example2.xlsx', engine='openpyxl')
data[0].drop('Unnamed: 0', axis=1).to_excel(writer, sheet_name='Sheet1', index=False)

writer.save()
return redirect('/')

if __name__ == "__main__":
app.run(debug=True)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
body { font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;}
a, h1, h2 { color: #377ba8; }
h1, h2 { margin: 0; }
h1 { border-bottom: 2px solid #eee; }
h2 { font-size: 1.2em; }

table.dataframe, .dataframe th, .dataframe td {
border: none;
border-bottom: 1px solid #C8C8C8;
border-collapse: collapse;
text-align:left;
padding: 10px;
margin-bottom: 40px;
font-size: 0.9em;
}



tr:nth-child(odd) { background-color:#eee; }
tr:nth-child(even) { background-color:#fff; }

tr:hover { background-color: #ffff99;}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!doctype html>
<head>
<title>Simple tables</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
<link rel=stylesheet type=text/css href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel=stylesheet type=text/css href="https://cdn.datatables.net/plug-ins/f2c75b7247b/integration/bootstrap/3/dataTables.bootstrap.css">
<!--<link rel=stylesheet type=text/css href="https://cdn.datatables.net/responsive/1.0.4/css/dataTables.responsive.css"> -->
</head>
<body>

<input type='button' id="sa" value='Save'></input>
<div class=page>
<!-- <h1>Python</h1> -->
{% for table in tables %}
<h2>{{titles[loop.index]}}</h2>
{{ table|safe }}
{% endfor %}
</div>
<form action="/insert" method="post">
<label>Num1</label> <input type="text" name="num1" placeholder="number1"></input>
<label>Num2</label> <input type="text" name="num2" placeholder="number2"></input>
<input type="submit" value="Insert"></input>
</form>





<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"> </script>
<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"> </script>
<script src="https://cdn.rawgit.com/ejbeaty/CellEdit/master/js/dataTables.cellEdit.js"> </script>
<script>
$(document).ready(function() {
var table = $('#example').DataTable();
table.MakeCellsEditable({
"onUpdate": myCallbackFunction
});
function myCallbackFunction(updatedCell, updatedRow, oldValue) {
console.log("The new value for the cell is: " + updatedCell.data());
console.log("The old value for that cell was: " + oldValue);
console.log("The values for each cell in that row are: " + updatedRow.data());
updatedCell.data();
updatedRow.data();
table.draw();
}
});
</script>
<script>
$("#sa").click(function() {
$.ajax({
url: '/save',
type: 'POST',
data : document.documentElement.innerHTML,
success: function(response) {
alert('Data is Saved')
}
})
});
</script>
</body>
</html>