Django показ ненужной информации - PullRequest
0 голосов
/ 16 апреля 2020

Мой скрипт python работает так, как я хочу, но по какой-то причине я получаю информацию, которая мне не нужна. enter image description here

На этом изображении я не хочу ничего печатать перед словом «острый». Мой код python получает статью из Википедии, обобщает ее и печатает лучшие предложения. Тем не менее, нет, где у меня есть код для печати других вещей, которые появляются. Как только кнопка генерирования нажата (в данном случае это так)

Вот мой код: 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,'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)

1 Ответ

1 голос
/ 16 апреля 2020

Опять же, это не вина Django, это показывает, что вы хотите показать. Сгенерированная ненужная информация находится в out. out является экземпляром subprocess.CompletedProcess. Вам необходимо вернуть stdout значение out:

def external(request):
    out=run([sys.executable,'textSummary.py'],shell=False,stdout=PIPE)

    return render(request,'index.html',{'data1':out.stdout})
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...