Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I output the results of a py script? #462

Closed
Uefi1 opened this issue Mar 11, 2024 · 9 comments
Closed

How can I output the results of a py script? #462

Uefi1 opened this issue Mar 11, 2024 · 9 comments
Labels

Comments

@Uefi1
Copy link

Uefi1 commented Mar 11, 2024

Hello, how can I output the results to a string variable of a .py script running in a multi-threaded application ?

function ExecPythonFile(const Filename):string;
var
FGILState: PyGILstate_STATE;
begin
Result := '';
FGILState := PythonEngine.PyGILState_Ensure;
try
PythonEngine.ExecFile(Filename);
except
PythonEngine.IO.Write(Result); //No WORK ((((
end;
PythonEngine.PyGILState_Release(FGILState);
end;
@Alexey-T
Copy link

Alexey-T commented Mar 11, 2024

Exec the script containing 'print( )' and use

property IO: TPythonInputOutput read FIO write FIO;               
property RedirectIO: Boolean read FRedirectIO write FRedirectIO default True;   

@Uefi1
Copy link
Author

Uefi1 commented Mar 11, 2024

Exec the script containing 'print( )' and use

Thanks, I figured it out a little but it doesn't work (

function ExecPythonFile(const Filename):string;
var
FGILState: PyGILstate_STATE;
PythonInputOutput:TPythonGUIInputOutput;
begin
Result := '';
PythonInputOutput:=TPythonGUIInputOutput.Create(nil);
PythonEngine.IO:=PythonInputOutput;
Result:=PythonInputOutput.Output.Text;
FGILState := PythonEngine.PyGILState_Ensure;
try
PythonEngine.DoRedirectIO;
PythonEngine.ExecFile(Filename);
except
end;
PythonEngine.PyGILState_Release(FGILState);
PythonInputOutput.Free;
end;

@Alexey-T
Copy link

Result:=PythonInputOutput.Output.Text;
PythonEngine.DoRedirectIO

I never used these 2 methods.
what I use:

Screenshot from 2024-03-11 13-24-58

@Uefi1
Copy link
Author

Uefi1 commented Mar 11, 2024

I never used these 2 methods.

Sorry I don't understand your code a bit (

@Alexey-T
Copy link

Alexey-T commented Mar 11, 2024

  • I make new object PythonIO
  • I set its event OnSendUniData (Uni - to get UnicodeString in event handler)
  • and I set PythonIO.UnicodeIO to True
  • I attach PythonIO to TPythonEngine.IO property

@Uefi1
Copy link
Author

Uefi1 commented Mar 11, 2024

  • I attach PythonIO to TPythonEngine.IO property

Still, I think that my last option should work, but I don’t understand why it doesn’t work (
Thank you very much for your help, of course, but you have some other language in which you write, so I don’t understand it yet

@pyscripter
Copy link
Owner

pyscripter commented Mar 11, 2024

  • You should not create and destroy TPythonGUIInputOutput every time you execute a script. This does not work. Create once before the python dll is loaded.
  • To output from threads you can use the DelayWrites property
  • Alternatively see the LessSimpleDemo in https://en.delphipraxis.net/topic/5686-python-code-is-running-in-main-thread-on-standard/
  • Use support forum for answers to questions, not the Issue tracker. If you do it again I will blacklist you from this repo.

@Uefi1
Copy link
Author

Uefi1 commented Mar 11, 2024

  • Use support forum for answers to questions, not the Issue tracker. If you do it again I will blacklist you from this repo.

Hello, I think I did everything as you said, the output still doesn’t work (

var
PythonEngine: TPythonEngine;
PythonInputOutput:TPythonGUIInputOutput;

procedure InitPython;
begin
  PythonEngine := TPythonEngine.Create(nil);
  try
    PythonInputOutput:=TPythonGUIInputOutput.Create(nil);
    PythonInputOutput.DelayWrites:=True;
    PythonEngine.InitThreads := True;
    PythonEngine.UseLastKnownVersion := True;
    PythonEngine.AutoLoad := False;
    PythonEngine.AutoFinalize := False;
    PythonEngine.AutoUnload := False;
    PythonEngine.RedirectIO := True;
    PythonEngine.IO:=PythonInputOutput;
    PythonEngine.LoadDll;
    TPythonThread.Py_Begin_Allow_Threads;
  finally
  end;
end;

function ExecPythonFile(const Filename:string):string;
var
FGILState: PyGILstate_STATE;
begin
Result := '';
FGILState := PythonEngine.PyGILState_Ensure;
try
PythonEngine.ExecFile(Filename);
except
end;
PythonEngine.PyGILState_Release(FGILState);
Result:=PythonInputOutput.Output.Text;
end;

Once again, the Issue tracker on GitHub exists for questions, ideas, feedback, tasks, or bugs for work on GitHub.:
https://docs.github.com/en/issues/tracking-your-work-with-issues/about-issues

Why the hell should I register on forums with dubious content for each individual repository on Github, and what if the database is stolen on such a forum?

@Uefi1
Copy link
Author

Uefi1 commented Mar 12, 2024

In general, the Python4Delphi BUG does not want to display data for any parameters or settings or assigned events onsenddata onreceivedata

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants