Skip to content

Commit 85f2348

Browse files
Added read write data with excel using flask in pandas (codebasics#12)
* Create flask_with_excel.py * Create style.css * Create index.html * Add files via upload
1 parent 74dcc4c commit 85f2348

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from flask import *
2+
import pandas as pd
3+
import os
4+
import re
5+
app = Flask(__name__)
6+
7+
@app.route("/")
8+
def show_tables():
9+
filename = 'example2.xlsx'
10+
data = pd.read_excel(filename,sheetname='Sheet1')
11+
data = data.fillna('')
12+
return render_template('index.html',tables=[re.sub(' mytable', '" id="example', data.to_html(classes='mytable'))],
13+
titles = ['Excel Data to Flask'])
14+
15+
16+
17+
@app.route('/insert', methods= ['POST','GET'])
18+
def insert():
19+
q1 = request.form['num1']
20+
q2 = request.form['num2']
21+
print(q1,q2)
22+
df = pd.DataFrame({'a': [q1],
23+
'b': [q2]})
24+
25+
book = pd.read_excel('example2.xlsx')
26+
writer = pd.ExcelWriter('example2.xlsx', engine='openpyxl')
27+
book.to_excel(writer, startrow=0, index=False)
28+
df.to_excel(writer, startrow=len(book) + 1, header=False, index=False)
29+
writer.save()
30+
return redirect('/')
31+
32+
@app.route('/save', methods= ['POST','GET'])
33+
def save():
34+
url = 'http://127.0.0.1:5000/'
35+
urll = request.get_data()
36+
print(urll)
37+
data = pd.read_html(urll)
38+
print(data)
39+
writer = pd.ExcelWriter('example2.xlsx', engine='openpyxl')
40+
data[0].drop('Unnamed: 0', axis=1).to_excel(writer, sheet_name='Sheet1', index=False)
41+
42+
writer.save()
43+
return redirect('/')
44+
45+
if __name__ == "__main__":
46+
app.run(debug=True)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
body { font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;}
2+
a, h1, h2 { color: #377ba8; }
3+
h1, h2 { margin: 0; }
4+
h1 { border-bottom: 2px solid #eee; }
5+
h2 { font-size: 1.2em; }
6+
7+
table.dataframe, .dataframe th, .dataframe td {
8+
border: none;
9+
border-bottom: 1px solid #C8C8C8;
10+
border-collapse: collapse;
11+
text-align:left;
12+
padding: 10px;
13+
margin-bottom: 40px;
14+
font-size: 0.9em;
15+
}
16+
17+
18+
19+
tr:nth-child(odd) { background-color:#eee; }
20+
tr:nth-child(even) { background-color:#fff; }
21+
22+
tr:hover { background-color: #ffff99;}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!doctype html>
2+
<head>
3+
<title>Simple tables</title>
4+
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
5+
<link rel=stylesheet type=text/css href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
6+
<link rel=stylesheet type=text/css href="https://cdn.datatables.net/plug-ins/f2c75b7247b/integration/bootstrap/3/dataTables.bootstrap.css">
7+
<!--<link rel=stylesheet type=text/css href="https://cdn.datatables.net/responsive/1.0.4/css/dataTables.responsive.css"> -->
8+
</head>
9+
<body>
10+
11+
<input type='button' id="sa" value='Save'></input>
12+
<div class=page>
13+
<!-- <h1>Python</h1> -->
14+
{% for table in tables %}
15+
<h2>{{titles[loop.index]}}</h2>
16+
{{ table|safe }}
17+
{% endfor %}
18+
</div>
19+
<form action="/insert" method="post">
20+
<label>Num1</label> <input type="text" name="num1" placeholder="number1"></input>
21+
<label>Num2</label> <input type="text" name="num2" placeholder="number2"></input>
22+
<input type="submit" value="Insert"></input>
23+
</form>
24+
25+
26+
27+
28+
29+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"> </script>
30+
<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"> </script>
31+
<script src="https://cdn.rawgit.com/ejbeaty/CellEdit/master/js/dataTables.cellEdit.js"> </script>
32+
<script>
33+
$(document).ready(function() {
34+
var table = $('#example').DataTable();
35+
table.MakeCellsEditable({
36+
"onUpdate": myCallbackFunction
37+
});
38+
function myCallbackFunction(updatedCell, updatedRow, oldValue) {
39+
console.log("The new value for the cell is: " + updatedCell.data());
40+
console.log("The old value for that cell was: " + oldValue);
41+
console.log("The values for each cell in that row are: " + updatedRow.data());
42+
updatedCell.data();
43+
updatedRow.data();
44+
table.draw();
45+
}
46+
});
47+
</script>
48+
<script>
49+
$("#sa").click(function() {
50+
$.ajax({
51+
url: '/save',
52+
type: 'POST',
53+
data : document.documentElement.innerHTML,
54+
success: function(response) {
55+
alert('Data is Saved')
56+
}
57+
})
58+
});
59+
</script>
60+
</body>
61+
</html>

0 commit comments

Comments
 (0)