jeudi 19 avril 2018

Catching Python Error on c#

I'm new to ASP.NET MVC and C#, so I've got a lot of doubts. What I'm trying to do is to run a Python script in one of my controllers and return the files generated by the script to the user. The input file is a .xlsx and the output is a zip containing the mesh and the figures generated by the script.

This part is working greatly, but I'm struggling to present the Python Errors on the website when there are.

This is the code that runs the Python script:

 public string run_cmd(string cmd, string arg1, string arg2) 
    {
        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = @"C:/Python27/python.exe"; // python.exe path
        start.Arguments = string.Format("{0} {1} {2}", cmd, arg1, arg2); //cmd: script path
        start.UseShellExecute = false;// Do not use OS shell
        start.CreateNoWindow = true; // We don't need new window
        start.RedirectStandardOutput = true;// Any output, generated by application will be redirected back
        start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script
                if (stderr != null){
                    return stderr;
                    // throw new System.InvalidOperationException("NOT VALID");
                }
                string result = reader.ReadToEnd(); // Here is the result of StdOut(for example: print "test")

                return "Success";

            }
        }
    }

And the controller that I use:

  public class IOController : Controller
{
    public string GetTemporaryDirectory()
    {
        string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
        Directory.CreateDirectory(tempDirectory);
        return tempDirectory;
    }

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {

        var tmpPath = Path.ChangeExtension(Path.GetTempFileName(),"xlsx");

        file.SaveAs(tmpPath);


        var tmpDirectory = GetTemporaryDirectory();

        Debug.WriteLine("A temporary directory was created");
        Debug.WriteLine(tmpDirectory);


        var tmpOutput= Path.GetFileNameWithoutExtension(Path.GetTempFileName());

        ViewBag.message=run_cmd(@"C:/Scripts/Python/main.py",tmpPath, Path.Combine(tmpDirectory,tmpOutput));



        ZipFile.CreateFromDirectory(tmpDirectory, tmpDirectory+".zip");
        var fileBytes = RunCommand(tmpPath);



        System.IO.File.Delete(tmpPath);



        return File(tmpDirectory+".zip", "application/zip","result.zip");


    }

The ultimate objective is to return the Error Message in stderr to the html, so the user can correct the input file:

<h1>Welcome to my site</h1>
<p class="simple_text">

    My site will generate your mesh!
</p>

<h2> Please upload your file here:</h2>

@using (Html.BeginForm("Index", "IO", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

    <input name="file" type="file" />

    <input type="submit" value="Generate" />}

Anyone have an idea?

Best regards!




Aucun commentaire:

Enregistrer un commentaire