Django显示不必要的信息

问题描述 投票:0回答:1

我让我的python脚本按照我想要的方式运行,但是由于某种原因,我收到一些我不想要的信息。 enter image description here

在此图像中,我不希望在“急性”一词之前打印任何内容。我的python代码获取了Wikipedia文章,对其进行了总结,并打印出最佳句子。但是,我没有代码可以打印其他正在显示的内容。单击生成按钮后(在本例中为单击)

这是我的代码:网址:

    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)
python django
1个回答
1
投票

同样,这不是Django的错,它在显示您想要显示的内容。产生的不必要信息在out中。 outsubprocess.CompletedProcess的实例。您需要返回subprocess.CompletedProcessstdout值:

stdout
© www.soinside.com 2019 - 2024. All rights reserved.