Embedded Python Block

This block allows you to create a new (custom) block in Python without needing to make and install an Out of Tree (OOT) Module. When you add the block to your flowgraph, the pre-populated code simply takes the input stream and multiplies it by a constant. Note that the structure of this Python block matches the structure of an OOT Python block. It's essentially a Python OOT block built into a grc flowgraph.

Initial code in the Python block
Here is the pre-populated code:

User notes
Important: For version 3.8, it is recommended that any flowgraph using an Embedded Python Block be placed in a separate directory (folder); particularly so if more than one Embedded Python Block is used in the same directory. This is because the generation process creates a file epy_block_0.py from that flowgraph. If you generate a different flowgraph in the same directory, its Embedded Python Block will overwrite the file. This problem has been resolved with a new naming convention in 3.9 which prepends the flowgraph name. For example "pkt_31_epy_block_0.py" Warning: To make changes to the Python code, you must follow the steps above. If you edit the epy_block_0.py file directly and then generate the flowgraph again, your changes will be wiped out! (The most recent edited code is actually stored in the .grc file.)
 * To revise the Python code:
 * right click on the Embedded Python block
 * select Properties
 * select Open in Editor
 * select Use Default or Choose Editor
 * For some platforms the Use Default button may do nothing; see below.
 * if you select Choose Editor, the /usr/bin folder will open for you to pick one.
 * When you have finished editing, save the file (usually Ctrl-S).
 * close the edit window.
 * If there are any syntax errors, they are shown in the Properties window.
 * select OK . It is not necessary to select Apply.
 * to change the default editor (works for Ubuntu and Debian):
 * open File Manager
 * single right click a file of type .py
 * left click on 'Properties'
 * click on the pull down for 'Open with:'
 * click on the editor of your choice
 * click OK

Parameters
The parameters for this block are simply those of your embedded Python block itself.

Note that every parameter needs a default value. For example, in the code above, note line:    If you don't have any parameters, change that line of code to

If you have a vector length equal to a variable of your block, note that the default value must be "hardcoded".

Example Flowgraph
This flowgraph shows how an Embedded Python Block can be used to convert a string from a Message Edit Box to a byte string for the output port. The text is sent through a Throttle to a File Sink. The file sink should have Unbuffered = On. In this particular case, the Throttle block is not required because all blocks will wait for a message string to be entered into the Message Edit Box. In this way the Message Edit Box limits the number of samples produced. Text output is in the black screen at the bottom of the image.



This is the epy_block_0.py code:

Another example can be found in https://github.com/duggabe/gr-morse-code-gen

Types of Python blocks
The pre-populated code shows the use of a gr.sync_block producing one data item of output for each data item of input (synchronous). Other classes are decim_block for decimation, interp_block for interpolation, and the generic basic_block which has no fixed relation between the number of input items and the number of output items produced.

More details can be found in Types_of_Blocks and BlocksCodingGuide. Even though Guided_Tutorial_GNU_Radio_in_C%2B%2B is written for the C++ programmer, the forecast, consume, history, and set_output_multiple functions used in a basic block are documented there.