ImportError when trying to embed a Bokeh section next to the Django download button

I am trying to embed a Bokeh plot in a Django site that has a download button on it. I use the example here here as a starting point, and then adding the implementation instructions from here , I used it need-a-minimal-django-file-upload-example/for_django_1.8, and it works as intended. Then I changed the following files, but I get the following error:

ImportError: no module named 'myapp'

Here you can see my directory structure and three files modified / added:

Directory structure

If you cannot upload the image, here:

.
└── myproject
    β”œβ”€β”€ db.sqlite3
    β”œβ”€β”€ manage.py
    β”œβ”€β”€ media
    β”‚   └── documents
    β”‚       └── 2016
    β”‚           └── 01
    β”‚               β”œβ”€β”€ 12
    β”‚               β”‚   └── silico_0_truth_filtered_ptps.csv
    β”‚               └── 15
    β”‚                   └── silico_0_truth_filtered_ptps.csv
    └── myproject
        β”œβ”€β”€ __init__.py
        β”œβ”€β”€ myapp
        β”‚   β”œβ”€β”€ admin.py
        β”‚   β”œβ”€β”€ forms.py
        β”‚   β”œβ”€β”€ __init__.py
        β”‚   β”œβ”€β”€ migrations
        β”‚   β”‚   β”œβ”€β”€ 0001_initial.py
        β”‚   β”‚   β”œβ”€β”€ __init__.py
        β”‚   β”‚   └── __pycache__
        β”‚   β”‚       β”œβ”€β”€ 0001_initial.cpython-35.pyc
        β”‚   β”‚       └── __init__.cpython-35.pyc
        β”‚   β”œβ”€β”€ models.py
        β”‚   β”œβ”€β”€ __pycache__
        β”‚   β”‚   β”œβ”€β”€ admin.cpython-34.pyc
        β”‚   β”‚   β”œβ”€β”€ admin.cpython-35.pyc
        β”‚   β”‚   β”œβ”€β”€ forms.cpython-35.pyc
        β”‚   β”‚   β”œβ”€β”€ __init__.cpython-34.pyc
        β”‚   β”‚   β”œβ”€β”€ __init__.cpython-35.pyc
        β”‚   β”‚   β”œβ”€β”€ models.cpython-34.pyc
        β”‚   β”‚   β”œβ”€β”€ models.cpython-35.pyc
        β”‚   β”‚   β”œβ”€β”€ urls.cpython-35.pyc
        β”‚   β”‚   └── views.cpython-35.pyc
        β”‚   β”œβ”€β”€ templates
        β”‚   β”‚   β”œβ”€β”€ list.html
        β”‚   β”‚   └── simple_chart.html
        β”‚   β”œβ”€β”€ tests.py
        β”‚   β”œβ”€β”€ urls.py
        β”‚   └── views.py
        β”œβ”€β”€ __pycache__
        β”‚   β”œβ”€β”€ __init__.cpython-34.pyc
        β”‚   β”œβ”€β”€ __init__.cpython-35.pyc
        β”‚   β”œβ”€β”€ settings.cpython-34.pyc
        β”‚   β”œβ”€β”€ settings.cpython-35.pyc
        β”‚   β”œβ”€β”€ urls.cpython-35.pyc
        β”‚   └── wsgi.cpython-35.pyc
        β”œβ”€β”€ settings.py
        β”œβ”€β”€ urls.py
        └── wsgi.py

Here templates/simple_chart.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Experiment with Bokeh</title>
    <script src="http://cdn.pydata.org/bokeh/release/bokeh-0.8.1.min.js"></script>
    <link rel="stylesheet" href="http://cdn.pydata.org/bokeh/release/bokeh-0.8.1.min.css">
</head>
<body>

    {{the_div|safe}}

    {{the_script|safe}}
</body>
</html>

Here myapp/urls.py(An error occurs here. I do not understand why the import cannot be allowed):

# -*- coding: utf-8 -*-
from django.conf.urls import patterns, url
from myapp.views import simple_chart

urlpatterns = patterns('myproject.myapp.views',
    url(r'^list/$', 'list', name='list'),
    url(r'^simple_chart/$', simple_chart, name="simple_chart"),
)

Here myapp/views.py:

# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

from myproject.myapp.models import Document
from myproject.myapp.forms import DocumentForm

from django.shortcuts import render
from bokeh.plotting import figure
from bokeh.resources import CDN
from bokeh.embed import components

def list(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile=request.FILES['docfile'])
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('myproject.myapp.views.list'))
    else:
        form = DocumentForm()  # A empty, unbound form

    # Load documents for the list page
    documents = Document.objects.all()

    # Render list page with the documents and the form
    return render_to_response(
        'list.html',
        {'documents': documents, 'form': form},
        context_instance=RequestContext(request)
    )

def simple_chart(request):
    plot = figure()
    plot.circle([1,2], [3,4])

    script, div = components(plot, CDN)

    return render(request, "simple_chart.html", {"the_script":script, "the_div":div})
+1
1

Django , , myapp/urls.py :

# -*- coding: utf-8 -*-
from django.conf.urls import patterns, url
from . import views

urlpatterns = patterns('myproject.myapp.views',
    url(r'^list/$', 'list', name='list'),
    url(r'^simple_chart/$', views.simple_chart, name="simple_chart"),
)

ImportError, , .

0

Source: https://habr.com/ru/post/1624514/


All Articles