如何将python中的多行返回到html?
我想打印出我评论的“打印出这行”的行。如您所见,我想在html上打印出5行。我只想在html页面上显示打印行,我不知道如何返回函数,也不确定返回值是否正确。我真的很困,不知道怎么做。我想在html页面中显示打印行,而不需要执行更多的函数来显示该行。我只知道如何将一行从python传递到html页面,但不知道如何使用多行如何将python中的多行返回到html?,python,django,Python,Django,我想打印出我评论的“打印出这行”的行。如您所见,我想在html上打印出5行。我只想在html页面上显示打印行,我不知道如何返回函数,也不确定返回值是否正确。我真的很困,不知道怎么做。我想在html页面中显示打印行,而不需要执行更多的函数来显示该行。我只知道如何将一行从python传递到html页面,但不知道如何使用多行 def output4(request): # Apply the vectorizer tfidf_matrix = vectorizer.fit_trans
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 %}
显示
{%b%}
{b|safe}
{%elif c%}
{{c | safe}}
{%endif%}
至于html页面,我也不确定我是否做对了。请先阅读:在一些列表或目录中收集您的值(这就是它们的用途-存储值集合),然后将它们传递给模板。如何将打印值放入列表中?