将Matplotlib图像渲染到烧瓶中

时间:2020-08-05 13:19:48

标签: python-3.x matplotlib flask

我有一个python脚本,可以使用dna_feature_viewer为蛋白质域生成图像,并且工作正常。我正在使用flask,并希望在网页中显示此图像。 enter image description here

我在脚本下面附加:

@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 )}}" />
   

运行flask脚本时,它将停止并显示以下错误:

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行。

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

1 个答案:

答案 0 :(得分:1)

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

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

也就是说,您的flask代码存在一些问题,我将在这里进行介绍。在您的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.pngsavefig方法仅使用此方法将文件实际保存到磁盘。
  • 然后我们将output_filename作为image_filename参数传递到模板,然后在模板中可用。

模板代码如下:

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

依次生成HTML:

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