0

This is my basic flask route

@app.route('/home/form', methods=('GET','POST'))            
def form():
    if request.method == 'POST':
        machine = request.form['machine']
        df = pd.DataFrame([[machine]], columns=["Machine"])
        with pd.ExcelWriter('P:\\Rohaan\\LATEST WEBSITE\\sample.xlsx') as writer:
            df.to_excel(writer)

        if not machine:
            flash('Title is required!')
        else:
            return redirect(url_for('home'))
    return render_template('form.html')

if __name__ == "__main__":
    app.run(debug=True)

enter image description here

My form.html that I retrieve data frame

HTML Code

<form method="post">
  <label for="Machine">Machine</label>
  <br>
  <input type="text" name="machine"
        placeholder="Message machine"
        value=''></input>
  <br>

  <label for="content">Message Content</label>
  <br>
  <textarea name="content"
            placeholder="Message content"
            rows="15"
            cols="60"
            ></textarea>
  <br>
  <button type="submit">Submit</button>
</form>

Currently getting this result using this code

enter image description here

My question is:

Why the incoming data override my previous data?

Yedidya Rashi
  • 1,012
  • 8
  • 18

3 Answers3

0

Currently getting this result using this code

@app.route('/home/form', methods=('GET','POST'))
def form():

if request.method == 'POST':
    machine = request.form['machine']
    df = pd.DataFrame([[machine]], columns=["Machine"])
    with pd.ExcelWriter('P:\\Rohaan\\LATEST WEBSITE\\sample.xlsx') as writer:
        df.to_excel(writer)

    if not machine:
        flash('Title is required!')
    else:
        return redirect(url_for('home'))
return render_template('form.html')
if name == "main": app.run(debug=True)

enter image description here

0

According to documentation you should specify mode = "a" if you want to append and not override (default is mode = "w") inside ExcelWriter, but if it is actually supported is probably dependent on the chosen excel engine.

Grekkq
  • 679
  • 6
  • 14
  • ValueError: Append mode is not supported with xlsxwriter! – Rohaan Sheikh Apr 21 '22 at 10:21
  • Exactly as i said it is dependent on used engine, you can refer to [this](https://stackoverflow.com/questions/54863238/pandas-excelwriter-valueerror-append-mode-is-not-supported-with-xlsxwriter) – Grekkq Apr 21 '22 at 10:34
0

HTML Code

<form method="post">
  <label for="Machine">Machine</label>
  <br>
  <input type="text" name="machine"
        placeholder="Message machine"
        value=''></input>
  <br>

  <label for="content">Message Content</label>
  <br>
  <textarea name="content"
            placeholder="Message content"
            rows="15"
            cols="60"
            ></textarea>
  <br>
  <button type="submit">Submit</button>
</form>