前提・実現したいこと
Python(Flask)を使い、Webサイト上でExcelファイルのダウンロード、画像ファイルの表示をさせたいです。
return文に直接タグを入れない方法を考え、タグを関数で小分けしそれを呼び出す方法をとることにしました。
発生している問題・エラーメッセージ
return文に「直接タグを入れる」または「関数を一つ返す」場合は上手くいくのですが、関数を二つ返すと下記のエラーが出力されます。
このページは動作していません 127.0.0.1 からデータが送信されませんでした。 ERR_EMPTY_RESPONSE
該当のソースコード
Python
1from flask import Flask, send_from_directory 2app = Flask(__name__) 3 4@app.route("/") 5def file_download(): 6 #return (r"<a href='\downloads\test.xlsx' download='test.xlsx'>Excelテンプレート-ダウンロード</a>" 7 # r"<img src='\downloads\graph.png' alt='グラフ' title='GRAPH'>") --- 成功 8 #return html_download() --- 成功 9 #return html_graph() --- 成功 10 #return html_download(), html_graph() --- 失敗 11 #return (html_download(), html_graph()) --- 失敗 12 13@app.route('/downloads/<path:filename>') 14def download_file(filename): 15 return send_from_directory('downloads', 16 filename, as_attachment=True) 17 18def html_download(): 19 html = r"<a href='\downloads\test.xlsx' download='test.xlsx'>Excelテンプレート-ダウンロード</a>" 20 return html 21 22def html_graph(): 23 html = r"<img src='\downloads\graph.png' alt='グラフ' title='GRAPH'>" 24 return html 25 26if __name__ == "__main__": 27 app.run(port=8000, debug=True)
試したこと
html_download関数とhtml_graph関数で同じ変数名を使用しているのが原因かと考え、html_d,html_gに変更しましたが問題は解消されませんでした。
環境
開発環境:PyCharm
サーバ :localhost
ブラウザ:Chrome
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/08 03:29