回答編集履歴

1

imgを表示する場合の例を追加

2020/12/08 02:21

投稿

FiroProchainezo
FiroProchainezo

スコア2401

test CHANGED
@@ -155,3 +155,161 @@
155
155
  回答が想定と異なる場合は、現象が再現する最小のコードを提示ください。(今の質問文のコードは実行できません。)
156
156
 
157
157
  その際は、tasojiroさんの頭の中だけにある情報を可能かぎり質問に記載ください。(実行環境、実行方法、ディレクトリ構造など)
158
+
159
+
160
+
161
+
162
+
163
+ ## 2020/12/08 11:12追記
164
+
165
+
166
+
167
+ imgで表示したい場合は、以下で可能です。
168
+
169
+ 上の`index.html`および下の`app.py`, `camera_capture.html`を使います。
170
+
171
+ jpgを置くフォルダは、`images`です。
172
+
173
+ 配置としては以下になります。
174
+
175
+ ```text
176
+
177
+ root
178
+
179
+ ├ images
180
+
181
+ │ ├ 1.jpg
182
+
183
+ │ └ 2.jpg
184
+
185
+ ├ templates
186
+
187
+ │ ├ index.html
188
+
189
+ │ └ camera_capture.html
190
+
191
+ └ app.py
192
+
193
+ ```
194
+
195
+
196
+
197
+ staticフォルダにimages(jpg)を配置する場合は、`/images`をBlueprintで追加しなくてOKです。
198
+
199
+
200
+
201
+ ```python
202
+
203
+ # app.py
204
+
205
+ from flask import Flask, render_template, Blueprint
206
+
207
+ import glob
208
+
209
+ import os
210
+
211
+
212
+
213
+ add_static = Blueprint('images', __name__, static_url_path='/images', static_folder='./images')
214
+
215
+ app = Flask(__name__)
216
+
217
+ app.register_blueprint(add_static)
218
+
219
+
220
+
221
+
222
+
223
+ def basename(path):
224
+
225
+ return os.path.basename(path)
226
+
227
+
228
+
229
+
230
+
231
+ app.jinja_env.filters['basename'] = basename
232
+
233
+
234
+
235
+
236
+
237
+ @app.route('/index')
238
+
239
+ def show_html():
240
+
241
+ return render_template('index.html')
242
+
243
+
244
+
245
+
246
+
247
+ @app.route('/camera_capture')
248
+
249
+ def camera_capture():
250
+
251
+ data = glob.glob('images/*.jpg')
252
+
253
+ return render_template('camera_capture.html', data=data)
254
+
255
+
256
+
257
+
258
+
259
+ if __name__ == '__main__':
260
+
261
+ app.run()
262
+
263
+ ```
264
+
265
+
266
+
267
+ ```html
268
+
269
+ <!-- camera_capture.html -->
270
+
271
+ <!DOCTYPE html>
272
+
273
+ <html lang="ja">
274
+
275
+ <head>
276
+
277
+ <meta charset="UTF-8">
278
+
279
+ <title>Title</title>
280
+
281
+ </head>
282
+
283
+ <body>
284
+
285
+ {% for n in data %}
286
+
287
+ <img src="{{ n }}" alt="{{ n|basename }}">
288
+
289
+ {% endfor %}
290
+
291
+ </body>
292
+
293
+ </html>
294
+
295
+ ```
296
+
297
+
298
+
299
+ なお、`app.py`の内、以下の部分については、imgにalt属性を追加する際にファイル名を指定したかったから追加したものなので読み飛ばしていただいてかまいません。
300
+
301
+ ```
302
+
303
+ import os
304
+
305
+ def basename(path):
306
+
307
+ return os.path.basename(path)
308
+
309
+
310
+
311
+
312
+
313
+ app.jinja_env.filters['basename'] = basename
314
+
315
+ ```