Я пытаюсь запустить код python нажатием кнопки html и запустить скрипт python. Я бы предпочел просто использовать обычную кнопку, а не форму для создания кнопки «отправить» (как у меня это для моей первой карты, которую я создал в index.html
), но с помощью учебника, который я последовал, это было сделано и не мог найти способ обойти это. Все, что я хочу сделать, это запустить скрипт, который я создал одним нажатием кнопки. Я пытаюсь сделать это с Django, но не могу исправить эту ошибку.
TypeError at /external/
bufsize must be an integer
Request Method: POST
Request URL: http://127.0.0.1:8002/external/
Django Version: 3.0.5
Exception Type: TypeError
Exception Value:
bufsize must be an integer
Exception Location: C:\Users\12673\anaconda3\lib\subprocess.py in __init__, line 702
Python Executable: C:\Users\12673\anaconda3\python.exe
urls:
from django.conf.urls import url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url, include
from django.urls import path
from . import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$',views.button),
url(r'^output',views.output,name="script"),
url(r'^external',views.external),
]
Views.py
from django.shortcuts import render
import requests
from subprocess import run,PIPE
import sys
def button(request):
return render(request,'index.html')
def output(request):
data=requests.get("https://regres.in/api/users")
print(data.text)
data=data.text
return render(request,'index.html',{'data':data})
def external(request):
out=run(sys.executable,['//c//Users//12673//desktop//project-04-story-spinner//djangoOutput//textSummary.py'],shell=False,stdout=PIPE)
print(out)
return render(request,'index.html',{'data1':out})
index. html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Card layout</title>
<link rel="stylesheet" type="text/css" href="{% static 'css/styles.css' %}"/>
<link href="https://fonts.googleapis.com/css2?family=Dancing+Script&display=swap" rel="stylesheet">
</head>
<body>
<h1 class="StorySpinner"><u>Story Spinner</u></h1>
<main>
<div class="container">
</div>
<section class="cards">
<div class="card">
<div class="card__image-container">
<img src="https://images.unsplash.com/photo-1528561156510-aba26bedef10?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80"/>
</div>
<div class="card__content">
<p class="card__title text--medium">
Watermelon Bicycle
</p>
<div class card="card__info">
<form action="/external/" method="post">
{% csrf_token %}
{{data1}}
<br><br>
<input type="submit" value="Generate">
</form>
</div>
</div>
</div><div class="card">
<div class="card__image-container">
<img src="https://images.unsplash.com/photo-1528561156510-aba26bedef10?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80"/>
</div>
<div class="card__content">
<p class="card__title text--medium">
Watermelon Bicycle
</p>
<div class card="card__info">
<button class="card__price text--medium">Select</button>
</div>
</div>
</div><div class="card">
<div class="card__image-container">
<img src="https://images.unsplash.com/photo-1528561156510-aba26bedef10?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80"/>
</div>
<div class="card__content">
<p class="card__title text--medium">
Watermelon Bicycle
</p>
<div class card="card__info">
<button class="card__price text--medium">Select</button>
</div>
</div>
</div><div class="card">
<div class="card__image-container">
<img src="https://images.unsplash.com/photo-1528561156510-aba26bedef10?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80"/>
</div>
<div class="card__content">
<p class="card__title text--medium">
Watermelon Bicycle
</p>
<div class card="card__info">
<button class="card__price text--medium">Select</button>
</div>
</div>
</div><div class="card">
<div class="card__image-container">
<img src="https://images.unsplash.com/photo-1528561156510-aba26bedef10?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80"/>
</div>
<div class="card__content">
<p class="card__title text--medium">
Watermelon Bicycle
</p>
<div class card="card__info">
<button class="card__price text--medium">Select</button>
</div>
</div>
</div><div class="card">
<div class="card__image-container">
<img src="https://images.unsplash.com/photo-1528561156510-aba26bedef10?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80"/>
</div>
<div class="card__content">
<p class="card__title text--medium">
Watermelon Bicycle
</p>
<div class card="card__info">
<button class="card__price text--medium">Select</button>
</div>
</div>
</div><div class="card">
<div class="card__image-container">
<img src="https://images.unsplash.com/photo-1528561156510-aba26bedef10?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80"/>
</div>
<div class="card__content">
<p class="card__title text--medium">
Watermelon Bicycle
</p>
<div class card="card__info">
<button class="card__price text--medium">Select</button>
</div>
</div>
</div><div class="card">
<div class="card__image-container">
<img src="https://images.unsplash.com/photo-1528561156510-aba26bedef10?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80"/>
</div>
<div class="card__content">
<p class="card__title text--medium">
Watermelon Bicycle
</p>
<div class card="card__info">
<button class="card__price text--medium">Select</button>
</div>
</div>
</div><div class="card">
<div class="card__image-container">
<img src="https://images.unsplash.com/photo-1528561156510-aba26bedef10?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80"/>
</div>
<div class="card__content">
<p class="card__title text--medium">
Watermelon Bicycle
</p>
<div class card="card__info">
<button class="card__price text--medium">Select</button>
</div>
</div>
</div><div class="card">
</div>
</section>
</main>
</main>
</body>
</html>
textSummary.py
import bs4 as bs
import urllib.request
import re
import nltk
import heapq
source = urllib.request.urlopen('https://en.wikipedia.org/wiki/Coronavirus')
soup = bs.BeautifulSoup(source,'lxml')
maxWordsAllowed=15#no sentence should have more words than this many
maxSentencesAllowed=2#no more than this many sentences allowed
#getting all paragraphs in the html
text = ""
for paragraph in soup.find_all('p'):
text += paragraph.text
#formatting the sentences
text = re.sub(r'\[[0-9]*\]',' ',text)
text = re.sub(r'\s+',' ',text)
#we want a clean text that has no periods commas etc as it would mess up our sentences
clean_text = text.lower()
clean_text = re.sub(r'\W',' ', clean_text)
clean_text = re.sub(r'\d',' ', clean_text)
clean_text = re.sub(r'\s+',' ', clean_text)
#tokenizing the text
sentences = nltk.sent_tokenize(text)
stop_words = nltk.corpus.stopwords.words('english')
word2count = {}#used for tracking score of the words
for word in nltk.word_tokenize(clean_text):#tokenize the words
if word not in stop_words:#if it is not a stop word
if word not in word2count.keys():#check to see if word is in the keys yet or not
word2count[word] = 1#new word
else:
word2count[word] += 1#inc this frequency by one
for key in word2count.keys():#going through the keys
word2count[key] = word2count[key]/max(word2count.values())#dividing this specific word's frequency by the max numbers frequency
sent2score = {}#used for sentence score
for sentence in sentences:#go through the sentences
for word in nltk.word_tokenize(sentence.lower()):#lowercase the words
if word in word2count.keys():#if the word is in
if len(sentence.split(' '))<maxWordsAllowed:#checks to see if the sentence is less than the specified
if sentence not in sent2score.keys():#if sentence is not in the list
sent2score[sentence] = word2count[word]#add the words frequency score to sent2score
else:
sent2score[sentence] += word2count[word]#sentence already in the list so we want to add the words score and update it
best_sentences = heapq.nlargest(maxSentencesAllowed,sent2score,key=sent2score.get)#this will specify how many sentences allowed, get the sentences with the best scores and use those
newString = ""
for sentence in best_sentences:#printing the best sentences
newString += sentence
print(newString)