前提・実現したいこと
Flaskで、「xml形式のファイル(拡張子は.eaf)をアップロードし、それを書き換えてダウンロードさせる」というアプリケーションを作ろうとしています。
発生している問題・エラーメッセージ
アプリケーションを立ち上げた直後1回目の処理はうまくいき、ファイルもダウンロードできます。しかし、その後の処理では、初回と全く同じ中身のファイルがダウンロードされてしまいます。
Pythonのソースコードの「#ファイルを保存」以降の部分が良くないと思うのですが、どうすればよいのかわかりません。
どのようにすれば、その回に処理したファイルをダウンロードできるでしょうか?
該当のソースコード
Python3
1# -*- coding: utf-8 -*- 2#Flaskとrender_template(HTMLを表示させるための関数)をインポート 3from flask import Flask,render_template,request,Markup,Response,make_response 4from werkzeug.utils import secure_filename 5import re,os 6import xml.etree.ElementTree as ET 7 8app = Flask(__name__) 9 10@app.route("/", methods=["get","post"]) # ルートにアクセスされたときの処理 11def entry(): 12 return render_template("index.html") 13 14@app.route('/upload', methods=['POST']) 15def upload(): 16 # テキストファイルの取得 17 txt_file = request.form['input_text'] 18 text = txt_file.splitlines() 19 text_list = [] 20 translation_list = [] 21 22 # 奇数行目をtextとして、偶数行目をtranslationとしてリストに加える 23 i = 1 24 for line in text: 25 if i % 2 == 1: 26 line = re.sub(r"([^ ])(-|=)",r"\1 \2",line) 27 line = re.sub(r"(,|.)","",line) 28 line = re.sub(r"^*","* ",line) 29 text_list.append(line) 30 elif i % 2 == 0: 31 translation_list.append(line) 32 i += 1 33 34 # eafファイルをリクエストし、パーシングしてルートを取る 35 tree = ET.parse(request.files["eaf_file"]) 36 # tree = ET.parse("app/static/14_受動表現.eaf") 37 root = tree.getroot() 38 39 # 指定された名前のtierの中身を取得 40 for child in root: 41 if child.tag == "TIER" and child.attrib["TIER_ID"] == request.form["tier_name_text"]: 42 content_tier_text = child 43 if child.tag == "TIER" and child.attrib["TIER_ID"] == request.form["tier_name_translation"]: 44 content_tier_translation = child 45 46 # テキストと訳の文章をeafに書き込む 47 i = 0 48 for child in content_tier_text: 49 child[0][0].text = text_list[i] 50 i += 1 51 if i == len(text_list): 52 break 53 54 i = 0 55 for child in content_tier_translation: 56 child[0][0].text = translation_list[i] 57 i += 1 58 if i == len(text_list): 59 break 60 61 #ファイルを保存 62 tree.write("app/templates/new.eaf","utf-8") 63 response = make_response() 64 response.data = render_template("new.eaf") 65 response.headers['Content-Type'] = 'application/xml' 66 response.headers['Content-Disposition'] = 'attachment;filename=new.eaf' 67 return response 68 69if __name__ == "__main__": 70 app.run(debug=True) 71
該当のソースコード
html
1<!DOCTYPE html> 2<html> 3 <head></head> 4 <body> 5 <form method="post" action="/upload" enctype="multipart/form-data"> 6 空の注釈を作ったeafファイルを選択する<br> 7 <input type="file" name="eaf_file"><br><br> 8 文が入った注釈層の名前を入力 9 <input type="text" name="tier_name_text" value="text"><br> 10 訳文が入った注釈層の名前を入力 11 <input type="text" name="tier_name_translation" value="tl"><br><br> 12 例文を奇数行目に、訳を偶数行目に入力してください。<br> 13 <textarea type="text" name="input_text" rows= "10" cols="40"></textarea> 14 <input type="submit" value="分析する"> 15 </form> 16 </body> 17</html> 18
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/09/16 10:49