Skip to content

Commit

Permalink
Added read write data with excel using flask in pandas (codebasics#12)
Browse files Browse the repository at this point in the history
* Create flask_with_excel.py

* Create style.css

* Create index.html

* Add files via upload
  • Loading branch information
raghavpatnecha authored Feb 23, 2020
1 parent 74dcc4c commit 85f2348
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
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>

0 comments on commit 85f2348

Please sign in to comment.