1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] asp.net c# webform passing parameters to a python script not running

Discussão em 'Python' iniciado por Stack, Outubro 7, 2024 às 02:53.

  1. Stack

    Stack Membro Participativo

    I found an example of how to pass parameters using PythonNet, but I am not able to get it to trigger my Python code. Developing on VS 2022. I'm wanting to (first) create a subdirectory if is doesn't already exist. The path and directory should be passed from C#.

    protected void btnPythonTest_Click(object sender, EventArgs e)
    {
    // full path of python interpreter
    string python = @"C:\Users\User\AppData\Local\Programs\Python\Python312\python.exe";

    // python app to call
    string myPythonApp = @"C:\OllamaDocs\MyTestPython.py";

    // Paramters to send to Python (location of docs and Chroma for this user
    string OllamDocsPath = @"C:\OllamaDocs\100\1\";
    string OllamaChromaFilename = "Chroma_100_1B";

    try
    {
    System.Diagnostics.ProcessStartInfo myProcessStart = new System.Diagnostics.ProcessStartInfo(python);
    myProcessStart.UseShellExecute = true;
    myProcessStart.RedirectStandardOutput = false;

    // pass arguments to python script
    myProcessStart.Arguments = myPythonApp + " " + OllamDocsPath + " " + OllamaChromaFilename;

    System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

    myProcess.StartInfo = myProcessStart;

    myProcess.Start();

    lblStatus.Text = myPythonApp + " Created " + OllamDocsPath + OllamaChromaFilename;

    }
    catch (Exception ex)
    {
    errorMessage = ex.Message;
    lblStatus.Text = errorMessage;

    }

    } // end PythonTest()


    My Python test script is located in the root of C: as follows (MyTestPython.py)

    import os import sys

    # Test Phase II

    #Get args from C#
    OllamaPath = sys.argv[0]
    OllamaChroma = sys.argv[1]

    # Define the directory for vector store persistence
    #persist_directory = "C:\\OllamaDocs\\100\\1\\chroma_db_100-1"
    persist_directory = OllamaPath

    # Check if the vector store already exists
    if not os.path.exists(persist_directory):
    print("Creating new Chroma Directory " + persist_directory)
    os.makedirs(persist_directory) else:
    # Load the existing Chroma vector store
    else:
    # Load the existing Chroma vector store
    print("path exists.")

    Continue reading...

Compartilhe esta Página