#python #django #plotly #dashboard
Вопрос:
Я создал 2 функции для создания графиков в своих проектах, по сути, представление вызывает эти функции для создания графиков, однако проблема в том, что один из графиков не отображается, но код точно такой же, как и у другого графика (исключение составляют имена столбцов).
views.py
@login_required def dashboard(request): c = Profile.objects.get(user=request.user) leads = Leads.objects.filter(agent_id = c) deals = Deal.objects.filter(agent_id=c) #Data for plot qs_leads = Leads.objects.filter( agent_id=c, status='Open') qs_deals = Deal.objects.filter( agent_id=c) df = read_frame(qs_leads) #Creates Plots plot_div_leads = plot_leads_data(qs_leads) plot_div_deals = plot_deals_data(qs_deals) if len(leads) == 0: context = {'leads': len(leads), 'deals': len(deals), } else: if len(deals) == 0: context = {'leads':leads, 'deals': len(deals), 'pot_div_leads':plot_div_leads} else: context = {'leads':leads, 'deals':deals, 'pot_div_leads':plot_div_leads, 'plot_div_deals':plot_div_deals} return render(request, 'account/dashboard.html', context)
Dashboard.html
{% extends "base.html" %} {% load static %} {% block title %}Dashboard{% endblock %} lt;meta name="viewport" content="width=device-width, initial-scale=1"gt; {% block content %} lt;h1gt;Dashboardlt;/h1gt; lt;pgt;Welcome to your dashboard.lt;/pgt; lt;h1gt; Add Data lt;/h1gt; lt;a href="{% url 'add_company' %}" class="buttons"gt;Add Companylt;/agt; lt;a href="{% url 'add_client' %}" class="buttons"gt;Add Clientlt;/agt; lt;a href="{% url 'add_lead' %}" class="buttons"gt;Add Leadlt;/agt; lt;a href="{% url 'add_call' %}" class="buttons"gt;Add Call Reportlt;/agt; lt;pgt;lt;/pgt; lt;h1gt;Your Data lt;/h1gt; lt;pgt;Click on the buttons inside the tabbed menu:lt;/pgt; lt;div class="tab"gt; lt;button class="tablinks" onclick="openOp(event, 'Leads')"gt;Leadslt;/buttongt; lt;button class="tablinks" onclick="openOp(event, 'Deals')"gt;Dealslt;/buttongt; lt;/divgt; lt;div id="Leads" class="tabcontent display-block"gt; {% if leads == 0 %} lt;h5gt; You have not created any leads - Click Add Leadlt;/h5gt; {% else %} lt;table class="sortable"gt; lt;theadgt; lt;trgt; lt;thgt;Project IDlt;/thgt; lt;thgt;Agentlt;/thgt; lt;thgt;Companylt;/thgt; lt;thgt;Countrylt;/thgt; lt;thgt;Servicelt;/thgt; lt;thgt;Est. # of Licenseslt;/thgt; lt;thgt;Est. Revenuelt;/thgt; lt;thgt;Est. Closing Datelt;/thgt; lt;thgt;Statuslt;/thgt; lt;thgt;Updatelt;/thgt; lt;/trgt; lt;/theadgt; lt;tbodygt; {% for lead in leads %} lt;trgt; lt;tdgt;lt;a href = "{% url 'leads_update' lead.project_id %}"gt;{{ lead.project_id }}lt;/agt;lt;/tdgt; lt;tdgt;{{ lead.agent }}lt;/tdgt; lt;tdgt;{{ lead.company }}lt;/tdgt; lt;tdgt;{{ lead.country }}lt;/tdgt; lt;tdgt;{{ lead.services }}lt;/tdgt; lt;tdgt;{{ lead.expected_licenses }}lt;/tdgt; lt;tdgt;{{ lead.expected_revenue }}lt;/tdgt; lt;tdgt;{{ lead.estimated_closing_date }}lt;/tdgt; lt;tdgt;{{ lead.status }}lt;/tdgt; lt;tdgt; {% if lead.status == 'Closed' %} ----- {% else %} lt;a href = "{% url 'close_lead' %}?project_id={{ lead.project_id }}"gt; Close lt;/agt; {% endif %} lt;/tdgt; lt;/trgt; {% endfor %} lt;/tbodygt; lt;/tablegt; lt;h2gt; Plots lt;/h2gt; {% autoescape off %} {{ plot_div_leads }} {% endautoescape %} {% endif %} lt;/divgt; lt;div id="Deals" class="tabcontent display-none"gt; {% if deals == 0 %} lt;h5gt; You don't have any deals lt;/h5gt; {% else %} lt;table class="sortable"gt; lt;theadgt; lt;trgt; lt;thgt;Project IDlt;/thgt; lt;thgt;Agentlt;/thgt; lt;thgt;Companylt;/thgt; lt;thgt;Countrylt;/thgt; lt;thgt;Servicelt;/thgt; lt;thgt;# of Licenseslt;/thgt; lt;thgt;Revenuelt;/thgt; lt;thgt;Closed Datelt;/thgt; lt;thgt;Commentslt;/thgt; lt;/trgt; lt;/theadgt; lt;tbodygt; {% for deal in deals %} lt;trgt; lt;tdgt;{{ deal.project_id }} lt;/tdgt; lt;tdgt;{{ deal.agent }}lt;/tdgt; lt;tdgt;{{ deal.company }}lt;/tdgt; lt;tdgt;{{ deal.country }}lt;/tdgt; lt;tdgt;{{ deal.service }}lt;/tdgt; lt;tdgt;{{ deal.licenses }}lt;/tdgt; lt;tdgt;{{ deal.revenue }}lt;/tdgt; lt;tdgt;{{ deal.closing_date }}lt;/tdgt; lt;tdgt;{{ deal.comments }}lt;/agt;lt;/tdgt; lt;/trgt; {% endfor %} lt;/tbodygt; lt;/tablegt; lt;div class="div-plot"gt; lt;h2gt;Plotslt;/h2gt; {% autoescape off %} {{ plot_div_deals|safe }} {% endautoescape %} lt;/divgt; {% endif %} lt;/divgt; lt;script type="text/javascript" src={% static "js/table.js" %}gt;lt;/scriptgt; lt;/divgt; {% endblock %}
The file for the plots
graphs = [] graphs.append(go.Bar( x=df['company'], y=df['expected_revenue'], name='Estimated Revenue' )) layout = { 'title': 'Estimated Revenue by Company', 'xaxis_title': 'Company', 'yaxis_title': 'Estimated Revenue', 'height': 500, 'width': 640, } plot_div_leads = plot({'data': graphs, 'layout': layout}, output_type='div',include_plotlyjs=False, show_link=False,link_text="") return plot_div_leads def plot_deals_data(qs): df = read_frame(qs) graphs = [] graphs.append(go.Bar( x=df['company'], y=df['revenue'], name='Estimated Revenue', marker=dict(color='#008375'), width=0.2, )) layout = { 'title': 'Revenue by Company', 'title_xanchor':'center', 'title_yanchor':'top', 'title_y':0.9, 'title_x':0.5, 'xaxis_title': 'Company', 'yaxis_title': 'Actual Revenue', 'height': 600, 'width': 740, 'font_family':'Muli', 'font_color':'#008375', 'font_size': 16 } plot_div = plot({'data': graphs, 'layout': layout}, output_type='div',include_plotlyjs=False, show_link=False,link_text="") return plot_div
The result is the following
Deals
У кого-нибудь есть идеи, почему работает только один сюжет?