简体   繁体   中英

make a button that will run .py

I'm making a website that will take 2 input files and return the differences between those file as an output. I already made the textcompare program in python and the first two webpages that will take the uploaded files and store it in one folder with the textcompare.py The last webpage that i'm having problem with has only one button that says execute. The main idea behind this button is when I click it, it will run textcompare.py, the program produces an output, then it will send it to the user to download it. Problem is, I dont know how to define a class that will run textcompare .py. I'm new to python and django so thanks for bearing with me.

In the execute.html I have

{% extends 'base.html' %}

{% load static %}

{% block content %}

    <div>
      <form action= '{% url my_view_name%}' method="POST">
          <input type="submit" value="Execute" id="Execute" />
      </form>
    </div>


  {% if uploaded_file_url %}

  {% endif %}


{% endblock %}

In the views.py I have

from django.shortcuts import render, redirect
from django.conf import settings
from django.core.files.storage import FileSystemStorage

from uploads.core.models import Document
from uploads.core.forms import DocumentForm


def home(request):
    documents = Document.objects.all()
    return render(request, 'core/home.html', { 'documents': documents })


def uploadOne(request):
    if request.method == 'POST' and request.FILES['myfile']:
        myfile = request.FILES['myfile']
        fs = FileSystemStorage()

        modified_name = "file1.csv".format()
        filename = fs.save(modified_name, myfile)


        #filename = fs.save(myfile.name, myfile)

        uploaded_file_url = fs.url(filename)
        return render(request, 'core/uploadTwo.html', {
            'uploaded_file_url': uploaded_file_url
        })
    return render(request, 'core/uploadOne.html')

def uploadTwo(request):
    if request.method == 'POST' and request.FILES['myfile']:
        myfile = request.FILES['myfile']
        fs = FileSystemStorage()

        modified_name = "file2.csv".format()
        filename = fs.save(modified_name, myfile)

        #filename = fs.save(myfile.name, myfile)

        uploaded_file_url = fs.url(filename)
        return render(request, 'core/uploadTwo.html', {
            'uploaded_file_url': uploaded_file_url
        })
    return render(request, 'core/uploadTwo.html')

def execute(request):
    if request.method == 'POST':
        import TextCompare.py


def my_view_name(request):
    #your script contents will go here. not literally but this is where gist of script should run
    # calculate the difference and put it in context var and return as follows
    diff = 
    # this program is to compare 2 files and extract the differences in a new file
    # the line old.csv, new.csv, and update.csv can be replaced to another type of file such as .txt
    with open('file1.csv', 'r') as t1, open('file2.csv', 'r') as t2: # here i open 2 files and make python read it
        file1 = t1.readlines() # read lines from 1st file
        file2 = t2.readlines() # read lines from 2nd file

    with open('difference.csv', 'w') as outFile: # create output file called with update.csv
        for line in filetwo: #for statement
            if line not in fileone: # if line is not same
                outFile.write(line) # print the output()

        context['diff'] = diff
    return render(request, 'core/uploadOne.html', context)

and urls.py I have

from django.conf.urls import url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

from uploads.core import views


urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^uploads/uploadOne/$', views.uploadOne, name='uploadOne'),
    url(r'^uploads/uploadTwo/$', views.uploadTwo, name='uploadTwo'),
    url(r'^uploads/execute/$', views.execute, name='execute'),
    url(r'^admin/', admin.site.urls),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Here is the textcompare.py

# this program is to compare 2 files and extract the differences in a new file
# the line old.csv, new.csv, and update.csv can be replaced to another type of file such as .txt
with open('file1.csv', 'r') as t1, open('file2.csv', 'r') as t2: # here i open 2 files and make python read it
    fileone = t1.readlines() # read lines from 1st file
    filetwo = t2.readlines() # read lines from 2nd file

with open('difference.csv', 'w') as outFile: # create output file called with update.csv
    for line in filetwo: #for statement
        if line not in fileone: # if line is not same
            outFile.write(line) # print the output

I believe the urls.py is good but views.py and execute.html is not ready. If anyone could help me defining the class and going through execute.html that'd be awesome. Thanks in advance

Okay it does not work like that. You are telling your form to go on uri my_view_name. Make aa route with this name in url.py. define a function which gets invoked on that url. Your url pattern would look like

from django.conf.urls import url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

from uploads.core import views


urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^uploads/uploadOne/$', views.uploadOne, name='uploadOne'),
    url(r'^uploads/uploadTwo/$', views.uploadTwo, name='uploadTwo'),
    url(r'^uploads/execute/$', views.execute, name='execute'),
    url(r'^my_view_name/$', views.my_view_name, name='my_view_name'),
    url(r'^admin/', admin.site.urls),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

In your views.py

def my_view_name(request):
    #your script contents will go here. not literally but this is where gist of script should run
    # calculate the difference and put it in context var and return as follows
    diff = yourscript()
    context['diff'] = diff
    return render(request, 'core/uploadOne.html', context)

PS: This is absolutely not the way to do this, Ideally it should be upload files together and calculate the difference right then, but Since I do not know extent of what your application is doing I would not get into that.

def yourscript():
    # You would need to give proper path for opening file on server, this might not work as it is
    with open('file1.csv', 'r') as t1, open('file2.csv', 'r') as t2: # here i open 2 files and make python read it
        fileone = t1.readlines() # read lines from 1st file
        filetwo = t2.readlines() # read lines from 2nd file

    with open('difference.csv', 'w') as outFile: # create 
    output file called with update.csv
        return_string = ''
        for line in filetwo: #for statement
            if line not in fileone: # if line is not same
                return_string+=line # print the output
        return return_string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM