0

I have the following code in python, which queries some values in a database and proceeds to graph them with matplotlib.

import sys
import pyodbc
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


if len(sys.argv) == 2:
    server = 'LOCAL'
    database = 'basic_db'
    try:
        conexion = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';Trusted_Connection=yes;')
        cursor = conexion.cursor()
                        
    except Exception as e:
        print("Error SQL Server: ",e)
    
    card = sys.argv[1]
    my_dpi= 96

    consulta = "SELECT qt_x from tb_value WHERE cd_card = "+str(card)+";"
    cursor.execute(consulta)
    img_x = cursor.fetchall()

    consulta = "SELECT qt_y from tb_value WHERE cd_card = "+str(card)+";"
    cursor.execute(consulta)
    img_y = cursor.fetchall()

    consulta = "SELECT max(qt_y) from tb_value WHERE cd_card = "+str(card)+";"
    cursor.execute(consulta)
    maxy = float(cursor.fetchval())
    y_tope = maxy + 1

    fig, catc = plt.subplots(figsize=(96/my_dpi,96/my_dpi))
    catc.plot(img_x,img_y, "black")

    catc.set_axis_off()
    limx= catc.get_xlim()
    limy= catc.get_ylim()
    catc.set_ylim((y_tope/2)*-1,y_tope)

    catc= plt.gcf()

    figname = 'cdg_{}.jpg'.format(card)
    path = r"C:\Users\basic user\Desktop\BSC\BasicProg\BasicProg\bin\Debug\cards"
    dest = os.path.join(path, figname)
    catc.savefig(dest, dpi=96)
    
else:
    print("Error - Introduce los argumentos correctamente")
    print("Ejemplo: graphbasiccard.py Carta ")

If I run the code in a cmd with: python graphbasiccard.py 1 it works perfectly. I have in this enviroment python 3.8.3.

The problem is that when trying to run the script by calling it from a C # function, the program does not return an error, but it simply does nothing. I am using the following code to make the call:

System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "/c \"python "+ @"C:\Users\Basic User\Desktop\Basic\BasicPRogram\BasicPRogram\bin\Debug\" + "graphbasiccard.py "+ cd_card;
            MessageBox.Show(startInfo.Arguments.ToString());
            process.StartInfo = startInfo;
            process.Start();

I also tried making a direct call to python.exe with:

System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = @"C:\Users\Anthony Godoy\AppData\Local\Programs\Python\Python38\python.exe";
            startInfo.Arguments = @"C:\Users\Basic User\Desktop\Basic\BasicPRogram\BasicPRogram\bin\Debug\graphbasiccard.py " + cd_card;
            process.StartInfo = startInfo;
            process.Start();

But nothing I can manage to make it work. Any advice?

2
  • Yes, please don't edit in the solution to the question itself. Commented Jan 8, 2021 at 19:04
  • oks guys, thx :) Commented Jan 8, 2021 at 19:14

1 Answer 1

1

I managed to fix it. The problem I had is that it gave a path away from the origin, I corrected it with the following:

ProcessStartInfo start = new ProcessStartInfo();
            start.FileName = @"C:\Users\Basic User\AppData\Local\Programs\Python\Python38\python.exe";
            start.Arguments = @"graphbasiccard.py " + cd_card;
            start.UseShellExecute = false;
            start.RedirectStandardOutput = true;
            using (Process process = Process.Start(start))
            {
                using (StreamReader reader = process.StandardOutput)
                {
                    string result = reader.ReadToEnd();
                    Console.Write(result);
                }
            }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.