【问题标题】:Rendering a matplotlib image into flask将 matplotlib 图像渲染到烧瓶中
【发布时间】:2023-04-07 01:08:01
【问题描述】:

我有一个 python 脚本,它使用 dna_feature_viewer 为蛋白质域生成图像并且工作正常。我正在使用烧瓶并希望在网页中显示此图像。

我附在脚本下面:

@app.route('/image' )  
def image():
 d={'domain1': ['4-50'], 'domain2': ['70-100']}
 from Bio import SeqIO
 from Bio.Seq import Seq
 from Bio.SeqRecord import SeqRecord
 from Bio.Alphabet import generic_protein
 from Bio.SeqFeature import SeqFeature, FeatureLocation
 from dna_features_viewer import BiopythonTranslator

 myseq='''MNEGFSEGEMETDRRTCSQQALHKDVEGKERRCQTCRSHLWLVALGLVLLSLTLCIFSLKYFWSPGPRKVYKHQYKVLLD
  GVEMDSVMEIDPNRLMEMFKVGNGSDEVLEVHDFKNGLTGI'''

 sequence_object = Seq(myseq, generic_protein)

 # Create a record
 record = SeqRecord(sequence_object,
               id='123456789', 
               name='Example',
               description='An example ')

 for keys, values in d.items():
   for i in range(len(values)):
      value_split_START=int(values[i].split('-')[0])
      value_split_END=int(values[i].split('-')[1])
      feature = SeqFeature(FeatureLocation(start=value_split_START, end=value_split_END), 
      type=keys)
      record.features.append(feature)


 graphic_record = BiopythonTranslator().translate_record(record)
 ax, _ = graphic_record.plot(figure_width=10, strand_in_label_threshold=7)
 
 image_filename=ax.figure.savefig("static/images/image.png")
 return render_template("image.html", imageout=image_filename)

脚本 2. Html 脚本

<img src="{{url_for('static', filename=image_filename )}}" />
   

在运行烧瓶脚本时,它会停止并显示以下错误:

WARNING: NSWindow drag regions should only be invalidated on the Main Thread! This will throw an exception in the future. Called from (
0   AppKit                              0x00007fff292cf607 -[NSWindow(NSWindow_Theme) _postWindowNeedsToResetDragMarginsUnlessPostingDisabled] + 378
1   AppKit                              0x00007fff292cc9f7 -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 1479
2   AppKit                              0x00007fff292cc42a -[NSWindow initWithContentRect:styleMask:backing:defer:] + 45
3   _macosx.cpython-37m-darwin.so       0x00000001233d283e -[Window initWithContentRect:styleMask:backing:defer:withManager:] + 94
4   _macosx.cpython-37m-darwin.so       0x00000001233d6745 FigureManager_init + 341
5   python                              0x000000010ed985ac wrap_init + 12
6   python                              0x000000010ed2255e wrapperdescr_call + 254
7   python                              0x000000010ed16ae3 _PyObject_FastCallKeywords + 179
8   python                              0x000000010ee53ed5 call_function + 453
9   python                              0x000000010ee51aec _PyEval_EvalFrameDefault + 46092
10  python                              0x000000010ed168d5 function_code_fastcall + 117
11  python                              0x000000010ed98381 slot_tp_init + 193
12  python                              0x000000010eda2361 type_call + 241
13  python                              0x000000010ed16ae3 _PyObject_FastCallKeywords + 179
14  python                              0x000000010ee53ed5 call_function + 453
15  python                              0x000000010ee51aec _PyEval_EvalFrameDefault + 46092
16  python                              0x000000010ed168d5 function_code_fastcall + 117
17  python                              0x000000010ee53dc7 call_function + 183
18  python                              0x000000010ee51aec _PyEval_EvalFrameDefault + 46092
19  python                              0x000000010ee4549e _PyEval_EvalCodeWithName + 414
20  python                              0x000000010ed15de7 _PyFunction_FastCallDict + 231
21  python                              0x000000010ed19ce2 method_call + 130
22  python                              0x000000010ed17752 PyObject_Call + 130
23  python                              0x000000010ee51d58 _PyEval_EvalFrameDefault + 46712
24  python                              0x000000010ee4549e _PyEval_EvalCodeWithName + 414
25  python                              0x000000010ed15de7 _PyFunction_FastCallDict + 231
26  python                              0x000000010ee51d58 _PyEval_EvalFrameDefault + 46712
27  python                              0x000000010ee4549e _PyEval_EvalCodeWithName + 414
28  python                              0x000000010ed16fe3 _PyFunction_FastCallKeywords + 195
29  python                              0x000000010ee53dc7 call_function + 183
30  python                              0x000000010ee51be0 _PyEval_EvalFrameDefault + 46336
31  python                              0x000000010ee4549e _PyEval_EvalCodeWithName + 414
32  python                              0x000000010ed16fe3 _PyFunction_FastCallKeywords + 195
33  python                              0x000000010ee53dc7 call_function + 183
34  python                              0x000000010ee51be0 _PyEval_EvalFrameDefault + 46336
35  python                              0x000000010ee4549e _PyEval_EvalCodeWithName + 414
36  python                              0x000000010ed15de7 _PyFunction_FastCallDict + 231
37  python                              0x000000010ee51d58 _PyEval_EvalFrameDefault + 46712
38  python                              0x000000010ed168d5 function_code_fastcall + 117
39  python                              0x000000010ee53dc7 call_function + 183
40  python                              0x000000010ee51a56 _PyEval_EvalFrameDefault + 45942
41  python                              0x000000010ed168d5 function_code_fastcall + 117
42  python                              0x000000010ee53dc7 call_function + 183
43  python                              0x000000010ee51a56 _PyEval_EvalFrameDefault + 45942
44  python                              0x000000010ed168d5 function_code_fastcall + 117
45  python                              0x000000010ee53dc7 call_function + 183
46  python                              0x000000010ee51a56 _PyEval_EvalFrameDefault + 45942
47  python                              0x000000010ed168d5 function_code_fastcall + 117
48  python                              0x000000010ed9646d slot_tp_call + 189
49  python                              0x000000010ed16ae3 _PyObject_FastCallKeywords + 179
50  python                              0x000000010ee53ed5 call_function + 453
51  python                              0x000000010ee51aec _PyEval_EvalFrameDefault + 46092
52  python                              0x000000010ed2fe49 gen_send_ex + 169
53  python                              0x000000010ee50c83 _PyEval_EvalFrameDefault + 42403
54  python                              0x000000010ee4549e _PyEval_EvalCodeWithName + 414
55  python                              0x000000010ed16fe3 _PyFunction_FastCallKeywords + 195
56  python                              0x000000010ee53dc7 call_function + 183
57  python                              0x000000010ee51b27 _PyEval_EvalFrameDefault + 46151
58  python                              0x000000010ee4549e _PyEval_EvalCodeWithName + 414
59  python                              0x000000010ed16fe3 _PyFunction_FastCallKeywords + 195
60  python                              0x000000010ee53dc7 call_function + 183
61  python                              0x000000010ee51a56 _PyEval_EvalFrameDefault + 45942
62  python                              0x000000010ed168d5 function_code_fastcall + 117
63  python                              0x000000010ee53dc7 call_function + 183
64  python                              0x000000010ee51a56 _PyEval_EvalFrameDefault + 45942
65  python                              0x000000010ed168d5 function_code_fastcall + 117
66  python                              0x000000010ee53dc7 call_function + 183
67  python                              0x000000010ee51aec _PyEval_EvalFrameDefault + 46092
68  python                              0x000000010ed168d5 function_code_fastcall + 117
69  python                              0x000000010ee53dc7 call_function + 183
70  python                              0x000000010ee51a56 _PyEval_EvalFrameDefault + 45942
71  python                              0x000000010ed168d5 function_code_fastcall + 117
72  python                              0x000000010ed98381 slot_tp_init + 193
73  python                              0x000000010eda2361 type_call + 241
74  python                              0x000000010ed16ae3 _PyObject_FastCallKeywords + 179
75  python                              0x000000010ee53ed5 call_function + 453
76  python                              0x000000010ee51aec _PyEval_EvalFrameDefault + 46092
77  python                              0x000000010ed168d5 function_code_fastcall + 117
78  python                              0x000000010ee53dc7 call_function + 183
79  python                              0x000000010ee51a56 _PyEval_EvalFrameDefault + 45942
80  python                              0x000000010ed168d5 function_code_fastcall + 117
81  python                              0x000000010ed19ce2 method_call + 130
82  python                              0x000000010ed17752 PyObject_Call + 130
83  python                              0x000000010ee51d58 _PyEval_EvalFrameDefault + 46712
84  python                              0x000000010ed168d5 function_code_fastcall + 117
85  python                              0x000000010ee53dc7 call_function + 183
86  python                              0x000000010ee51a56 _PyEval_EvalFrameDefault + 45942
87  python                              0x000000010ed168d5 function_code_fastcall + 117
88  python                              0x000000010ee53dc7 call_function + 183
89  python                              0x000000010ee51a56 _PyEval_EvalFrameDefault + 45942
90  python                              0x000000010ed168d5 function_code_fastcall + 117
91  python                              0x000000010ed19ce2 method_call + 130
92  python                              0x000000010ed17752 PyObject_Call + 130
93  python                              0x000000010ef358cb t_bootstrap + 123
94  python                              0x000000010eebc707 pythread_wrapper + 39
95  libsystem_pthread.dylib             0x00007fff57da22eb _pthread_body + 126
96  libsystem_pthread.dylib             0x00007fff57da5249 _pthread_start + 66
97  libsystem_pthread.dylib             0x00007fff57da140d thread_start + 13

)127.0.0.1 - - [05/Aug/2020 13:46:32] "/image HTTP/1.1" 200 - 断言失败:(NSViewIsCurrentlyBuildingLayerTreeForDisplay() != currentBuildingLayerTree),函数 NSViewSetCurrentlyBuildingLayerTreeForDisplay,文件 /BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1671.60.107/AppKit.subproj/NSView.m,第 14221 行。

我不知道为什么会这样。我将非常感谢任何帮助。 谢谢!

【问题讨论】:

  • 请显示完整的异常回溯。
  • 谢谢。我已经更新了
  • 这能解决问题吗? stackoverflow.com/a/53686262/2052575
  • 谢谢。我会试试这个。
  • 这里我不清楚如何使用subplot函数。

标签: python-3.x matplotlib flask


【解决方案1】:

我无法重新创建此异常,因此我认为它可能是您的环境所特有的。

我在官方的python docker 镜像中对此进行了测试,该镜像在 OSX 上的 Docker Desktop 中运行。

也就是说,我将在此处介绍您的烧瓶代码存在一些问题。在您的 image 函数即将结束时,您可能应该做一些类似的事情:

    # ....
    ax, _ = graphic_record.plot(figure_width=10, strand_in_label_threshold=7)
 
    # This bit changes...

    output_filename = "image.png"
    output_path = os.path.join('static', output_filename)

    # The return value of this is not a filename
    image = ax.figure.savefig(output_path)

    # You should actually be passing `output_filename` to the template
    return render_template("image.html", image_filename=output_filename)

请注意:

  • output_filename 是您要将图像另存为的实际文件名
  • output_path 是完整路径 static/image.png。这仅由savefig 方法用于将文件实际保存到磁盘。
  • 然后,我们将 output_filename 作为 image_filename 参数传递给模板,然后该参数在模板中可用。

模板代码如下:

<img src="{{url_for('static', filename=image_filename )}}" />

这又会生成 HTML:

<img src="/static/image.png" />

【讨论】:

    猜你喜欢
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 2019-04-22
    • 2018-03-25
    • 2013-12-05
    • 1970-01-01
    • 2021-02-22
    相关资源
    最近更新 更多