Skip to main content
Plug-in Item Created Edited

Guiding for writing Python Code (Planning/Development Collaboration)

 

To expedite the development of Plug-in items without extensively understanding the planner’s intent, the planner can shorten this time by creating Python code that contains their intent.

I’d like to propose structuring the files into three simple categories. Even if we only define ‘main’ and ‘components’ well, could we proceed with development directly with just design and Python code, even without a design document? (There’s no evidence, but it seems plausible, right?)

 

🚥 Basic Rules


  • The currently available Python version on the webpage is 3.11.2.

  • All Python code should be encapsulated within functions.

  • Instead of directly executing code, define def do() and perform testing by executing do().

    # General main.py
    a = 1
    b = 1
    value = a + b
    print(value)
    # Functionalized main.py
    def summation(a, b):
      return a + b
    # If you comment out the following three lines, the Python code won't execute immediately. a = 1
    b = 1
    summation(a, b)

     

  • Write Python code for the data to be filled in the UI. The data entering the UI can have various intentions. If you want to fill only the ‘X' value of the' NODE' data into a Drop List, you can write it as follows.

    # components.py
    def getNodeX4CompDropList():
      civil = MidasAPI(Product.CIVIL, "KR")
      nodeEntries = civil.dbRead("NODE")
      nodeXs = []
      for entry_id, entry_values in nodeEntries.items():
        x_value = entry_values.get("X")
        if x_value is not None:
          nodeXs.append(x_value)
      
      return nodeXs # [1, 2, 3, ...] X value of the NODE

    Actually, I mentioned filtering for the X-Coordinate as a stark example, but the condition could be a design standard or even concrete and steel. If there’s Python code containing the intention for the data to be filled in the UI, solving the issue of populating that value into the Drop List completes the Node 'X' Drop List!

  • Functions to be included in main.py should be written using parameters. For example, let’s assume there’s a main logic that doubles the X value of a specific Node selected from the previously created Drop List. If we have a selected Node X, write it in the following way by passing it as a parameter, which makes it easier to complete the plug-in item. The operation can be performed correctly by passing only the value of the selected Node X from the Drop List to the main. If I were to write the code, an example would look like this.

    # main.py
    def main(selectedNodeX):
      result = selectedNodeX * 2
      print(result)
    # Code for the test
    selectedNodeX = 1 # DropList Assumed that selected 1 in (Node X)
    main(selectedNodeX) # output displays as 2


📁 Python Files 3 Categories


main.py

Define the Python code that retrieves values from the UI and ultimately executes.

# main.py
from multiple import calc2x
def main(selectedNodeX):
  result = calc2x(selectedNodeX)
  print(result)
# Code for the test
selectedNodeX = 1 # DropList Assumed that selected 1 in (Node X)
main(selectedNodeX) # output displays as 2

 

components.py

Define the values to be filled in the UI components. Returning actual data allows for inferring and directly populating the UI without explanations from the design document.

# components.py
def getNodeX4CompDropList():
  civil = MidasAPI(Product.CIVIL, "KR")
  nodeEntries = civil.dbRead("NODE")
  
  nodeXs = []
  for entry_id, entry_values in nodeEntries.items():
    x_value = entry_values.get("X")
    if x_value is not None:
      nodeXs.append(x_value)
return nodeXs # [1, 2, 3.2, ...] NODE X values

 

sub_logics.py ← Free to set this name.

Define auxiliary logics to operate the main function.

If all the code is in the main function, it’s not easy for others to read the code.

It can be organized into one or multiple files.

# multiple.py
def calc2x(value):
  return value * 2

 

🌐 Python Code Testing Guide


You can explore each available library, but running Python code in the Plug-in Item development envirionment leads to Python code that can be immediately applied!

1) Install an extension for testing Python through a web page

Install the ‘Live Server’ Extension in VSCode. This extension enables immediate execution by simply saving Python Code. Its role is to launch a local server to open the web page.
1.png

 

2) Pulling the engineers-api-python github.

Adding a new directory called ‘pyscript_tester’.
2.png

 

3) To start a new project, create a new folder by copying the ‘pyscript_tester.’

3.png

 

4) Open the pasted folder in VSCode within the project.

4.png

 

5) Click on ‘index.html’ in the left tree menu and then click on ‘Go Live’ at the bottom right.

If Live Server is installed correctly, you should see ‘Go Live’ at the bottom right.
5.png

 

6) If the browser opens and you encounter an image like the one below, your test environment setup is successful!

6.png

  • The basic structure involves executing ‘pyscript_main.py.’

  • If you want to create and use auxiliary Python files, create the auxiliary file and add its filename to ‘pyscript_config.json’!

7.png


Now, feel free to modify Python as you proceed with your work! 🙂

0
Was this article helpful?