Как вернуть несколько строк от python до html? - PullRequest
0 голосов
/ 15 января 2020

Я хочу распечатать строки, которые я прокомментировал, «распечатать эту строку». как вы можете видеть, есть 5 строк, которые я хотел напечатать на html. Я просто хочу, чтобы строка печати отображалась на странице html, и я не знаю, как вернуться к функции, и не уверен, верно ли мое возвращаемое значение. Я действительно застрял и не знаю, как это сделать. Я хотел бы отобразить строку печати на странице html, не выполняя больше функции для отображения строки. Я только знаю, как передать одну строку со страницы python на html, но не знаю, как это сделать с несколькими строками.

def output4(request):

    # Apply the vectorizer
    tfidf_matrix = vectorizer.fit_transform(raw_files)
    print(tfidf_matrix.shape) #print out this line
    terms = vectorizer.get_feature_names()

    from sklearn.cluster import KMeans, MeanShift, estimate_bandwidth, SpectralClustering, AffinityPropagation

    num_clusters = 5
    km = KMeans(n_clusters=num_clusters)
    km.fit(tfidf_matrix)
    clusters = km.labels_.tolist()
    novels = { 'title': filenames, 'text': raw_files, 'cluster': clusters }
    df = pd.DataFrame(novels, index = [clusters] , columns = ['title', 'text', 'cluster'])
    #sort cluster centers by proximity to centroid
    order_centroids = km.cluster_centers_.argsort()[:, ::-1] 
    for i in range(num_clusters):
        if i != 0:
            print ("Cluster %d words: " % i, end='') #print out this line

        for j, ind in enumerate(order_centroids[i, :10]):
            if (j == 0):
                b = '%s' % vocab_df.loc[terms[ind].split(' ')].values.tolist()[0][0]
                print (b) #print out this line
            else:
               c= '%s' % vocab_df.loc[terms[ind].split(' ')].values.tolist()[0][0]
               print (c) # print out this line

        print("Cluster %d titles: " % i, end='')
        for j, title in enumerate(df.loc[i]['title'].values.tolist()):
            if (j == 0):
                d= '%s' % title
            else:
                e= ', %s' % title
                print (e) #print out this line

    return render(request, 'clust.html', {'b':b},{'c':c},{'d':d}, {'e':e})

Вот url.py :

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r'^$', views.button),
    path(output4, views.output4,name="script4"),
]

Вот страница html:

<button onclick="location.href='{% url 'script4' %}'">display</button> <hr>
    {% if b %}
        {{b | safe}}
    {% elif c %}
        {{c | safe}}
    {% endif %}

как для html страница, я также не уверен, правильно ли я делаю

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...