0

I'm trying to embed a Bokeh plot into a Django site that has an upload button. I'm using the example given here as a starting point and then adding on the instructions on embedding from here. I used need-a-minimal-django-file-upload-example/for_django_1.8 and it works as intended. I've then modified the following files, but I get the following error:

ImportError: No module named 'myapp'

You can see my directory structure here and the three files modified/added:

Directory structure

If you cannot load 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 is templates/simple_chart.html:

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

    {{the_div|safe}}

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

Here is myapp/urls.py (The error originates here. I can't understand why the import cannot be resolved):

# -*- 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 is 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})
bigreddot
  • 33,642
  • 5
  • 69
  • 122
Frikster
  • 2,755
  • 5
  • 37
  • 71
  • I'm confused. How is my question not an existing programming problem? – Frikster Jan 12 '16 at 11:32
  • Its not a programming problem, there isn't any problem here to solve, you're looking for someone to write code for you – Sayse Jan 12 '16 at 11:33
  • Is my question really much different from this one? http://stackoverflow.com/questions/5871730/need-a-minimal-django-file-upload-example EDIT: updated the title if that helps – Frikster Jan 12 '16 at 11:35
  • 3
    That question is around 4 years old and the community as a whole has changed since then, if it makes you feel any better I've voted to close that one too. – Sayse Jan 12 '16 at 11:36
  • ok... what about how my question explicitly states that I'm open to getting high level help if that is what whoever with more experience thinks I need? To meet this new SO standard, should I remove all mention of asking for writing an example from my question? – Frikster Jan 12 '16 at 11:40
  • Assumed yes. Question amended accordingly. – Frikster Jan 12 '16 at 12:01
  • I've modified the question to a single problem as required and will continue to edit later today. I found this which should help me improve the question further: https://github.com/bokeh/bokeh/tree/master/examples/embed – Frikster Jan 12 '16 at 21:06
  • 1
    Removed my comment, thanks for improving your question. –  Jan 12 '16 at 21:10

1 Answers1

0

After spending a few days ploughing through the Django tutorial I saw how they imported views there and thus I changed myapp/urls.py to be as follows:

# -*- 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"),
)

This removes the ImportError, though I cannot explain why.

Frikster
  • 2,755
  • 5
  • 37
  • 71