]> SALOME platform Git repositories - modules/gui.git/blob - doc/salome/gui/input/working_with_python_console.rst
Salome HOME
e0d84ffbb24eba870fd5cec8f4478fad7e1fe027
[modules/gui.git] / doc / salome / gui / input / working_with_python_console.rst
1 .. _python_console_page: 
2
3 ******************
4 Python Console
5 ******************
6
7 **Python console** - Window for Python interpreter. This window functions like a standard document:
8 the pop-up menu invoked by right-click in this window gives access to **Copy/Paste/SelectAll/ClearAll** options.
9
10 You can run a Python script in interactive mode by typing expressions line by line or by loading a Python file
11 from the main menu **File -> Load Script**.
12
13 ==================
14 Asynchronous mode
15 ==================
16
17 By default the console is always initialized in asynchronous mode when the Python commands are executed in the separated thread
18 that does not block the main GUI loop. So, you'll see any intermediate output from the running script even if the script didn't finish yet.
19
20 In the synchronous mode each Python command is executed in the main GUI thread, then GUI is blocked until the command is finished.
21 It could be an issue if you run a time consuming Python script, because you won't see any output till the end of execution.
22
23 If you need to run a script in synchronous mode for whatever reason, set ``PYTHON_CONSOLE_SYNC`` environment variable before Salome start:
24
25 .. code-block:: console
26
27     export PYTHON_CONSOLE_SYNC=1
28
29 ==================
30 Tracing
31 ==================
32
33 To output in console currently executed functions, we're adding tracing code to the start and at the end
34 of the command to deactivate it immediately after execution.
35
36 This mechanism is turned off by default. Set ``PYCONSOLE_TRACE`` before SALOME start to activate it:
37
38 .. code-block:: console
39
40     export PYCONSOLE_TRACE=1
41
42 If the tracing is on while the script is running we can see functions enter and return calls
43 with ``>>`` and ``<<`` marks followed by line number and function name. Tracing function prints only calls to functions
44 defined in the current script to prevent printing of thousands of lines for builtin functions in some cases.
45
46 Example of a script with defined functions:
47
48 .. code-block:: python
49     :linenos:
50
51     #!/usr/bin/env python
52
53     import time
54
55     def sum(x, y):
56         time.sleep(3)
57         return x + y
58         
59     def sub(x, y):
60         time.sleep(3)
61         return x - y
62         
63     def prod(x, y):
64         time.sleep(3)
65         return x * y
66         
67     def div(x, y):
68         time.sleep(3)
69         return x / y
70
71     x = 5
72     y = 2
73
74     sum(x, y)
75     sub(x, y)
76     prod(x, y)
77     div(x, y)
78
79 And output in the Python console:
80
81 .. code-block:: console
82
83     >>> sys.setprofile(lambda frame, event, arg: print('>>', frame.f_lineno, ': ', frame.f_code.co_name) if event == 'call' and frame.f_code.co_filename == '/home/function_calls.py' and frame.f_code.co_name != '<module>' else print('<<', frame.f_lineno, ': ', frame.f_code.co_name) if event == 'return' and frame.f_code.co_filename == '/home/function_calls.py' and frame.f_code.co_name != '<module>' else None); exec(compile(open('/home/function_calls.py', 'rb').read(), '/home/function_calls.py', 'exec')); sys.setprofile(None); 
84     >> 5 :  sum
85     << 7 :  sum
86     >> 9 :  sub
87     << 11 :  sub
88     >> 13 :  prod
89     << 15 :  prod
90     >> 17 :  div
91     << 19 :  div
92     >>>
93