How to get the PyMechanical app.print_tree() into logger info.?

Naveen Kumar Begari
Naveen Kumar Begari Member Posts: 77
10 Comments 5 Likes First Answer First Anniversary
**

Hi team, Im trying to get the app.print_tree() tree data into the logger. Below is the script for you reference:

`import os
import logging

os.environ['PYTHONIOENCODING'] = 'utf8'

from ansys.mechanical.core import App

logger = logging.getLogger()

logger.setLevel(logging.DEBUG)

stream_handler = logging.StreamHandler(sys.stdout)

stream_handler.setLevel(logging.DEBUG)

logger.addHandler(stream_handler)

from ansys.mechanical.core import App

app = App()

app =App(version=242)

ansys_tree = ansys_app.print_tree()

logger.debug(f"Ansys Tree Structure:\n{ansys_tree }")
`

But Im unable to get the tree.

I need your assistance here..!!

Comments

  • Naveen Kumar Begari
    Naveen Kumar Begari Member Posts: 77
    10 Comments 5 Likes First Answer First Anniversary
    **

    @Naveen Kumar Begari said:
    Hi team, Im trying to get the app.print_tree() tree data into the logger. Below is the script for you reference:

    `import os
    import logging

    os.environ['PYTHONIOENCODING'] = 'utf8'

    from ansys.mechanical.core import App

    logger = logging.getLogger()

    logger.setLevel(logging.DEBUG)

    stream_handler = logging.StreamHandler(sys.stdout)

    stream_handler.setLevel(logging.DEBUG)

    logger.addHandler(stream_handler)

    from ansys.mechanical.core import App

    app = App()

    app =App(version=242)

    ansys_tree = ansys_app.print_tree()

    logger.debug(f"Ansys Tree Structure:\n{ansys_tree }")
    `

    But Im unable to get the tree.

    I need your assistance here..!!

    Take this as reference:

    `import os
    import logging

    os.environ['PYTHONIOENCODING'] = 'utf8'

    from ansys.mechanical.core import App

    logger = logging.getLogger()

    logger.setLevel(logging.DEBUG)

    stream_handler = logging.StreamHandler(sys.stdout)

    stream_handler.setLevel(logging.DEBUG)

    logger.addHandler(stream_handler)

    from ansys.mechanical.core import App

    app = App()

    app =App(version=242)

    ansys_tree = app.print_tree()

    logger.debug(f"Ansys Tree Structure:\n{ansys_tree }")
    `

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 347
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    Hi @Naveen Kumar Begari . In your code, ansys_treeis not a string. There is probably a more elegant solution, but here's one workaround:

    tempfile = r'D:\temp.txt'
    import sys
    from ansys.mechanical.core import App
    app = App()
    
    original_stdout = sys.stdout
    with open(tempfile, 'w', encoding="utf-8") as file:
       sys.stdout = file
       app.print_tree()
       sys.stdout = original_stdout
    
    with open(tempfile, encoding="utf-8") as file:
        ansys_tree = file.read()
    
  • Naveen Kumar Begari
    Naveen Kumar Begari Member Posts: 77
    10 Comments 5 Likes First Answer First Anniversary
    **

    Hello @Landon Mitchell Kanner, Thank you so much for your response.

    But,
    when I execute these lines:

    ```python
    ansys_app = App(version=242)
    ansys_app.update_globals(globals())
    ansys_tree = ansys_app.print_tree()
    tempfile = 'simulation_logs.txt'
    original_stdout = sys.stdout
    with open(tempfile, 'a', encoding="utf-8") as file:
    sys.stdout = file
    ansys_app.print_tree()
    sys.stdout = original_stdout
    with open(tempfile, encoding="utf-8") as file:
    ansys_trees = file.read()

    So in .txt file it was reading perfect but in webapp it was showing different.


  • Naveen Kumar Begari
    Naveen Kumar Begari Member Posts: 77
    10 Comments 5 Likes First Answer First Anniversary
    **

    For you reference:

    ansys_app = App(version=242)
    ansys_app.update_globals(globals())
    ansys_tree = ansys_app.print_tree()
    tempfile = 'simulation_logs.txt'
    original_stdout = sys.stdout
    with open(tempfile, 'a', encoding="utf-8") as file:
        sys.stdout = file
        ansys_app.print_tree()
        sys.stdout = original_stdout
    with open(tempfile, encoding="utf-8") as file:
        ansys_trees = file.read()
    
  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 347
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    What is your ultimate goal?

  • Naveen Kumar Begari
    Naveen Kumar Begari Member Posts: 77
    10 Comments 5 Likes First Answer First Anniversary
    **

    @Landon Mitchell Kanner it's working fine, thanks for your support.

  • Naveen Kumar Begari
    Naveen Kumar Begari Member Posts: 77
    10 Comments 5 Likes First Answer First Anniversary
    **

    @Landon Mitchell Kanner said:
    What is your ultimate goal?

    from log file I need to show on the WebApp.

  • Naveen Kumar Begari
    Naveen Kumar Begari Member Posts: 77
    10 Comments 5 Likes First Answer First Anniversary
    **

    @Landon Mitchell Kanner, While performing the analysis (specifically when the solver is triggered), the logger keeps disconnecting.

    Is there a way to keep it active on the web app?

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 347
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    While performing the analysis (specifically when the solver is triggered), the logger keeps disconnecting.

    Is there a way to keep it active on the web app?

    I am not very familiar with the logging module, which is part of Python and not developed by Ansys. What are you using to create a web app? I suggest you post a new question with enough details to reproduce the issue.

  • Naveen Kumar Begari
    Naveen Kumar Begari Member Posts: 77
    10 Comments 5 Likes First Answer First Anniversary
    **

    @Landon Mitchell Kanner said:

    While performing the analysis (specifically when the solver is triggered), the logger keeps disconnecting.

    Is there a way to keep it active on the web app?

    I am not very familiar with the logging module, which is part of Python and not developed by Ansys. What are you using to create a web app? I suggest you post a new question with enough details to reproduce the issue.

    Hi @Landon Mitchell Kanner, I'm using Dash technology for webapp development.
    Sure, I will post