Диаграмма Боке в Django возвращает пустую страницу - PullRequest
0 голосов
/ 01 апреля 2019

Я пытаюсь пройти учебник , чтобы показать простую диаграмму боке на странице django, но веб-страница пуста, когда загружается - диаграммы нет.

Был похожий вопрос переполнения стека, где в html-файле была указана неправильная версия bokeh - я проверил, чтобы убедиться, что это не так. Я также попытался использовать функцию рендеринга, поскольку render_to_response, видимо, устарела, но произошло то же самое.

<!DOCTYPE html>
<html lang='en'>
    <head>
        <link href="http://cdn.pydata.org/bokeh/release/bokeh-1.0.4.min.css" rel="stylesheet" type="text/css">
        <link href="http://cdn.pydata.org/bokeh/release/bokeh-widgets-1.0.4.min.css" rel="stylesheet" type="text/css">
        <script src="http://cdn.pydata.org/bokeh/release/bokeh-1.0.4.min.js"></script>
        <script src="http://cdn.pydata.org/bokeh/release/bokeh-widgets-1.0.4.min.js"></script>
        {{ script | safe }}
        <title>testing bokeh...</title>
    </head>

    <body>
        {{ div | safe }}
    </body>
</html>
from django.shortcuts import render, render_to_response
from bokeh.plotting import figure, output_file, show
from bokeh.embed import components
def bokehTutorial(request):
    x, y, = [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]
    #Setup graph plot
    plot = figure(title = 'Line Chart', x_axis_label = 'X axis', y_axis_label = 'Y axis', plot_width = 400, plot_height = 400)
    #plot line
    plot.line(x, y, line_width = 2)
    #store components
    script, div = components(plot)
    #return to django homepage with components sent as arguments which will then be displayed
    return render_to_response('appName/bokehTutorial.html', {'script': script, 'div': div})
    #return render(request, 'appName/bokehTutorial.html', {'script': script, 'div': div})

Я ожидаю, что веб-страница покажет линейный график. Однако при загрузке веб-страница выглядит пустой.

Ответы [ 2 ]

0 голосов
/ 11 апреля 2019

Я получаю синтаксическую ошибку для последней строки.Мысли?return render ('fish / ticket_class.html', {'resources' = INLINE.render (), 'script': script, 'div': div})

0 голосов
/ 01 апреля 2019

Я думаю, вам нужно сначала поставить div, потому что script ищет его, чтобы вставить в него код.Также просто используйте INLINE.render() или CDN.render(), чтобы автоматически связать ресурсы (если вы не знаете, что делаете).Попробуйте следующее:

index.html

<!DOCTYPE html>
<html lang='en'>
    <head>
        {{ resources | safe }}
        <title>testing bokeh...</title>
    </head>  
    <body>
        {{ div | safe }}
        {{ script | safe }}
    </body>
</html>

django_app.py

from django.shortcuts import render, render_to_response
from bokeh.plotting import figure, output_file, show
from bokeh.embed import components
from bokeh.resources import INLINE

def bokehTutorial(request):
    x, y, = [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]
    #Setup graph plot
    plot = figure(title = 'Line Chart', x_axis_label = 'X axis', y_axis_label = 'Y axis', plot_width = 400, plot_height = 400)
    #plot line
    plot.line(x, y, line_width = 2)
    #store components
    script, div = components(plot)
    #return to django homepage with components sent as arguments which will then be displayed
    return render_to_response('appName/bokehTutorial.html', {'resources' = INLINE.render(), 'script': script, 'div': div})
...