title: Django Biginner - How to connect my algorithm code in django
body: <p>This might be simple, but I'm quite new to django. I want to build app to make the user upload a csv file, run my algorithm on it, and then make the output file available to be downloaded by the user. I already started my uploading file thing and it looks like it's working, and I already have my algorithm code ready, however, I'm not sure how to connect my algorithm code, also not sure how to make another function to include that, and do I need a new url for each uploading,running the algorithm and downloading? what I would like is one URL that displays the three of them. Here is my project files and app, the <code>model.py</code>:</p>
  
  <pre><code>from django.db import models
  from django.forms import ModelForm
  
  class Upload(models.Model):
      pic = models.FileField("CSV File", upload_to="images/")
      upload_date=models.DateTimeField(auto_now_add =True)
  
  # FileUpload form class.
  class UploadForm(ModelForm):
      class Meta:
          model = Upload
          fields = '__all__'  # Or a list of the fields that you want to include in your form
  </code></pre>
  
  <p><strong><code>View.py:</code></strong></p>
  
  <pre><code>from __future__ import division
  from django.shortcuts import render
  from uploader.models import UploadForm,Upload
  from io import TextIOWrapper
  from io import StringIO
  from django.http import HttpResponseRedirect
  from django.core.urlresolvers import reverse
  import csv
  import logging
  def home(request):
      if request.method=="POST":
          img = UploadForm(request.POST, request.FILES)
          if img.is_valid():
              img.save()
            #  request.FILES['pic'].open("f")
            #  portfolio = csv.DictReader(request.FILES['uploadFile'].file)
  
             #file =request.FILES['pic']
              file1 = TextIOWrapper(request.FILES['pic'].file)
             # reader = StringIO(file.read().decode())
              reader = file1.read()
              file1.seek(0)
              portfolio = csv.DictReader(reader)
              print(portfolio)
              #with open(reader) as f:
              my_list=[]
              import pdb;pdb.set_trace()
            #  with open(portfolio) as f:
              my_list = [row["BASE_NAME"] for row in portfolio]
            #  for row in portfolio:
               #   my_list = [row["BASE_NAME"]]
                #
              print(my_list)
              logging.warning('Watch out!')  # will print a message to the console
  
           #   for row in reader:
           #       print (row)
          #else:
           #   print (img.errors)
           #   print  (request.FILES)
             # img.save()
              #file = request.FILES[pic]
             # with open(file) as f:
                 # my_list = [row["BASE_NAME"] for row in DictReader(f)]
  
              return HttpResponseRedirect(reverse('imageupload'))
      else:
          img=UploadForm()
      images=Upload.objects.all()
      return render(request,'home.html',{'form':img,'images':images})
  </code></pre>
  
  <p>My <code>home.html</code> file:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;Title&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div style="padding:40px;margin:40px;border:1px solid #ccc"&gt;
      &lt;h1&gt;Upload the CSV File&lt;/h1&gt;
      &lt;form action="#" method="post" enctype="multipart/form-data"&gt;
          {% csrf_token %} {{form}}
          &lt;input type="submit" value="Upload" /&gt;
      &lt;/form&gt;
      {% for img in images %}
          {{forloop.counter}}.&lt;a href="{{ img.pic.url }}"&gt;{{ img.pic.name }}&lt;/a&gt;
          ({{img.upload_date}})&lt;hr /&gt;
      {% endfor %}
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And my <code>url.py</code></p>
  
  <pre><code>from django.conf.urls import url
  from django.contrib import admin
  from django.conf import settings
  from django.conf.urls.static import static
  
  urlpatterns = [
      url(r'^upload/$', 'uploader.views.home', name='imageupload'),
      url(r'^admin/', admin.site.urls),
  ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  </code></pre>
  
  <p><code>My algorithm code</code> I want to implement is:</p>
  
  <pre><code>with open("export.csv") as f:
      my_list = [row["BASE_NAME"] for row in DictReader(f)]
  # CodeCSvModel.import_from_file(file)
  vectorizer = CountVectorizer()
  dtm = vectorizer.fit_transform(my_list)
  
  lsa = TruncatedSVD(n_components=100)
  dtm_lsa = lsa.fit_transform(dtm)
  dtm_lsa = Normalizer(copy=False).fit_transform(dtm_lsa)
  similarity = np.asarray(numpy.asmatrix(dtm_lsa) * numpy.asmatrix(dtm_lsa).T)
  k = len(my_list)
  dist1 = np.subtract(np.ones((k, k), dtype=np.float), similarity)
  data2 = np.asarray(dist1)
  arr_3d = data2.reshape((1, k, k))
  
  no_clus = 1500
  for i in range(len(arr_3d)):
      km = AgglomerativeClustering(n_clusters=no_clus, linkage='average').fit(arr_3d[i])
  
      labels = km.labels_
  
  csvfile = file
  csv_input = pd.read_csv(csvfile)
  csv_input['cluster_ID'] = labels
  csv_input['BASE_NAME'] = my_list
  csv_input.to_csv('output.csv', index=False)
  clus_groups = list()
  for j in range(no_clus):
      list_of_ints = ([my_list[i] for i, x in enumerate(labels) if x == j])
      clus_groups.append('  '.join(list_of_ints))
  vectorizer = CountVectorizer()
  dtm = vectorizer.fit_transform(my_list)
  
  lsa = TruncatedSVD(n_components=100)
  dtm_lsa = lsa.fit_transform(dtm)
  dtm_lsa = Normalizer(copy=False).fit_transform(dtm_lsa)
  similarity = np.asarray(numpy.asmatrix(dtm_lsa) * numpy.asmatrix(dtm_lsa).T)
  k = len(my_list)
  dist1 = 1 - similarity
  data2 = np.asarray(dist1)
  arr_3d = data2.reshape((1, k, k))
  
  no_clus = 5
  for i in range(len(arr_3d)):
      km = KMeans(n_clusters=5, init='k-means++')
      km = km.fit(arr_3d[i])
      labels2 = km.labels_
  
  labels = labels.tolist()
  labels2 = labels2.tolist()
  
  csv_input = pd.read_csv('output.csv')
  labels1 = csv_input['cluster_ID']
  new_list = []
  for k in labels1:
      new_list.append(labels2[k])  # lookup the value in list2 at the index given by list1
  csv_input = pd.read_csv('output.csv')
  csv_input['cluster_ID'] = labels
  csv_input['BASE_NAME'] = my_list
  csv_input['User_Map'] = new_list
  csv_input.to_csv('output1.csv', index=False)
  </code></pre>
  
  <p>I already tried in my view.py to make <code>my_list</code> and to start, however not sure if I got my_list right since no print statements are showing, I also want to make a function inside the view to implement my algorithm above, and how to link the file I got from <code>home</code> function with the new function of the algorithm, I will also need another function for the downloading thing, and not sure about the templates. I'm kinda lost and did the uploading function by an example online. I appreciate the help.</p>