如何调用python脚本并将输出图像返回到View

问题描述

我在python中有一个wordcloud,我想使用MVC调用python .py并在我的View显示输出。 现在,我已经可以在c#中调用python .py,它可以成功运行并显示wordcloud。 但是我不知道如何在我的wordcloud显示View输出
我已引用此页面How do I execute and return the results of a python script in c#?

这是我的wordcloud.py

# -*- coding: utf-8 -*-
from wordcloud import WordCloud,STOPWORDS,ImageColorGenerator
import matplotlib.pyplot as plt
import pyodbc,time
import pandas as pd
import numpy as np
import sys
import jieba
import jieba.posseg as pseg
from io import BytesIO
import base64
from datetime import  datetime,timedelta
from collections import Counter
from PIL import Image


def wc():
    #temp=sys.argv[1]
    #print("ssssssssssssssssssssssssssssssssssss")
    server = 'LAPTOP-22AVH2GL\MSsqlSERVER01'
    #server = 'WILLY\sqlEXPRESS'
    database = 'Project2'
    uid = 'sa'
    pwd = 'neil0627'
    #pwd = 'h24664034'
    cnxn = pyodbc.connect('DRIVER={sql Server};SERVER='+server+';DATABASE='+database+';UID='+uid+';PWD='+pwd)
    cursor = cnxn.cursor()
    #sqlcom = "SELECT disTINCT JIEBA,COUNT(JIEBA) FROM Jieba WITH(NOLOCK) GROUP BY jieba" #News.api #Project
    sqlcom = "SELECT disTINCT JIEBA,COUNT(JIEBA) FROM Jieba1 WITH(NOLOCK) GROUP BY jieba" #News.api #Project2
    news = pd.read_sql(sqlcom,con=cnxn) 
    print(news)
    df3 = np.array(news)
    print(df3)
    df4 = dict(df3.tolist())
    print(df4)
    
    font_path = r'D:\anaconda3\Lib\site-packages\wordcloud\SimHei.ttf'
    #mask = np.array(Image.open("D:/456.png"))
    # back_color = imageio.imread("./taiwan.png")
    #myWordClode = WordCloud(width=850,height=850,background_color="white",#                          font_path=font_path,colormap="Dark2",mask=mask)
    myWordClode = WordCloud(width=850,font_path=font_path,colormap="Dark2")
    myWordClode.generate_from_frequencies(df4)
    # use PIL show the wordcloud
    fig = plt.figure()
    #fig = plt.figure(figsize=(20,10))
    plt.imshow(myWordClode)
    plt.axis("off")
    #fig.savefig("testtttttt.png")
    plt.show()
    #sio = BytesIO()
    #plt.savefig(sio,format='png')
    #data = base64.encodebytes(sio.getvalue()).decode()
    #print(data)
    
    myWordClode.to_file('wcp1.png')
wc()









这是我的控制器

private string run_cmd(string cmd,string args) { processstartinfo start = new processstartinfo(); start.FileName = @"C:/Users/user/AppData/Local/Programs/Python/python38-32/python.exe"; start.CreateNowindow = true; start.Arguments = string.Format("{0} {1}",cmd,args); start.UseShellExecute = false; start.RedirectStandardOutput = true; using (Process process = Process.Start(start)) { using (StreamReader reader = process.StandardOutput) { string result = reader.ReadToEnd(); //Console.Write(result); //string stderr = process.StandardError.ReadToEnd(); //string stdout = process.StandardOutput.ReadToEnd(); //Debug.WriteLine("STDERR: " + stderr); //Debug.WriteLine("STDOUT: " + stdout); return result; } } }

我尝试使用Viewbag将图像传递到View。我不知道这是否正确。这是我的代码

ViewBag.wordcloud = run_cmd("D:/Python/wordcloudjieba.py","Some Input");

有人做过吗? 谢谢你的想法!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)