Bokeh & Django: RuntimeError("模型必须只属于一个文档")
Posted
技术标签:
【中文标题】Bokeh & Django: RuntimeError("模型必须只属于一个文档")【英文标题】:Bokeh & Django: RuntimeError("Models must be owned by only a single document") 【发布时间】:2021-12-18 21:49:27 【问题描述】:我正在使用 python bokeh 和 django 制作一些散景图。我试图将这些散景图排成一排,但这失败了:
script, div = components(row(sample_plot, variant_plot, sizing_mode='scale_width'))
在下面的代码中。如果我尝试渲染使用此方法生成的单个图,它可以工作。我得到的错误信息是:
当我把我的地块排成一排时,我很困惑为什么会发生这种情况。
ERROR (exception.py:118; 04-11-2021 16:56:58): Internal Server Error: /
Traceback (most recent call last):
File "/home/hts_django/config/env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/home/hts_django/config/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/hts_django/config/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/hts_django/config/env/lib/python3.6/site-packages/django/views/generic/base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "/home/hts_django/config/env/lib/python3.6/site-packages/django/views/generic/base.py", line 89, in dispatch
return handler(request, *args, **kwargs)
File "/path/2/ma/viewz/HTS/views.py", line 932, in get
script, div = components(plot_row)
File "/home/hts_django/config/env/lib/python3.6/site-packages/bokeh/embed/standalone.py", line 216, in components
with OutputDocumentFor(models, apply_theme=theme):
File "/usr/lib/python3.6/contextlib.py", line 81, in __enter__
return next(self.gen)
File "/home/hts_django/config/env/lib/python3.6/site-packages/bokeh/embed/util.py", line 134, in OutputDocumentFor
doc.add_root(model)
File "/home/hts_django/config/env/lib/python3.6/site-packages/bokeh/document/document.py", line 321, in add_root
self._pop_all_models_freeze()
File "/home/hts_django/config/env/lib/python3.6/site-packages/bokeh/document/document.py", line 1104, in _pop_all_models_freeze
self._recompute_all_models()
File "/home/hts_django/config/env/lib/python3.6/site-packages/bokeh/document/document.py", line 1127, in _recompute_all_models
a._attach_document(self)
File "/home/hts_django/config/env/lib/python3.6/site-packages/bokeh/model.py", line 692, in _attach_document
raise RuntimeError("Models must be owned by only a single document, %r is already in a doc" % (self))
RuntimeError: Models must be owned by only a single document, Figure(id='1080', ...) is already in a doc
视图(带有生成图的示例函数)
def create_variant_barchart():
""" Create a sample tracking barchart for all sites.
args: None
returns: plot
"""
crs_qs = ClinicallyReportedSample.objects.filter(
second_interpret=False,
final_technical_report=True,
reported_on_panel__parent_panel__isnull=False,
diagnostic_report_required=True)
first_analysis = crs_qs.filter(
final_technical_check=True,
first_report=False)
crv_qs = ClinicallyReportedVariant.objects.filter(
clinically_reported_sample_id__in=first_analysis,
technical_status='valid')
num_variants = [o.final_vasr_id.variant_annotation_id.\
variant_gene_transcript_id.variant_id.id for o in crv_qs]
unique_variants = list(set(num_variants))
classified_variants = VariantClassification.objects.filter(
variant_annotation_id__variant_gene_transcript_id__variant_id__in=unique_variants,
reviewed_by=True)
to_classify_variants = len(unique_variants) - len(classified_variants)
x_axis = ['Samples', 'Variants']
counts = [len(first_analysis), to_classify_variants]
p = figure(
title="Unclassified Variants",
x_range=x_axis,
height=350,
toolbar_location=None,
tools="hover",
tooltips="@counts"
)
# p.vbar(x=x_axis, top=counts, width=0.9)
source = ColumnDataSource(data=dict(index=x_axis, counts=counts, color=Spectral6))
p.vbar(x='index',
top='counts',
width=0.9,
color='color',
source=source
)
p.y_range.start = 0
return p
class DashoardView(View):
def get(self, request, *args, **kwargs):
sample_plot = create_barchart_all_sites()
variant_plot = create_variant_barchart()
script, div = components(row(sample_plot, variant_plot, sizing_mode='scale_width'))
self.context['script'] = script
self.context['div'] = div
return render(request, self.template, self.context)
模板
<div class="row">
<div class="col mb-3">
<div id='outstanding_samples' class='card'>
<div class="card-header bg-light">
<h6><b>HODB Overview</b></h6>
</div>
<div class='container-fluid card-body'>
div|safe
script|safe
</div>
</div>
</div>
</div>
【问题讨论】:
【参考方案1】:我更改了下面两个函数中的变量名称,因为它们都将各自的图保存为 p .... bokeh 显然不喜欢它们相同!
sample_plot = create_barchart_all_sites()
variant_plot = create_variant_barchart()
【讨论】:
以上是关于Bokeh & Django: RuntimeError("模型必须只属于一个文档")的主要内容,如果未能解决你的问题,请参考以下文章