question

Nick avatar image
1 Like"
Nick asked Nick commented

How to get Python to read a Table in FlexSim?

I hope everyone is having a great week so far. I am seeking a solution that allows me to easily import a FlexSim table into Python. My objective is to execute a Python code that reads data from a FlexSim table and stores it in a dataframe for further data processing tasks. Currently, I have a script that exports global tables to a .csv file when prompted, which meets my requirements. However, I believe it would be more efficient to eliminate the intermediate step and create a Python function that directly retrieves data from a FlexSim model. This streamlined approach would save time and eliminate the need to manage "juggling .csv" files during subsequent validation processes in Python. I would like to know if it is possible to achieve this.

FlexSim 23.1.2
pythonflexsim python
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jeanette F avatar image Jeanette F ♦♦ commented ·

Hi @Nick, was Julie Weller's answer helpful? If so, please click the "Accept" button at the bottom of their answer. Or if you still have questions, add a comment and we'll continue the conversation.

If we haven't heard back from you within 3 business days we'll auto-accept an answer, but you can always comment back to reopen your question.

0 Likes 0 ·

1 Answer

Julie Weller avatar image
3 Likes"
Julie Weller answered Nick commented

Hey @Nick!

The answer to your question depends on whether you want to be based in Python or in FlexSim. If you want to call FlexSim from your python model, this git page should be able to explain that process:

https://github.com/flexsim/FlexSimPy

However, if you would like to call python from FlexSim it's pretty easy to call python functions. First make sure your python code is in the same folder as your FlexSim model, then go to toolbox -> User Commands. You can name your function anything you want but make sure your parameter match for example if I want to pass in an array to this function which sums a table in python.

1685052164819.png

Next click the custom code button on the far right (it looks like a little scroll) and at the bottom of the page that pops up click the E button:


screenshot-2023-05-25-152542.pngThen click format for python and yes. Now you can access you file and function by editing the two quoted text "PyMod" and "PyFunc" to be your file name and function respectively for example my file was named Test.py and my function was sumTablePy then I would put in this:

1685052337152.png

Before you use this function make sure to click apply on both the user commands tab and the code tab.

After I do that I can call my new function in the script tab on FlexSim by going to the script tab (on the command bar by tree and backgrounds) and call that with my global list as the table using code that looks something like this

Array data;
Table table = Table("GlobalTable1");
table.cloneTo(Table(data));
return sumTable(data);


I hope that helps!


· 3
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Nick avatar image Nick commented ·

Hey Julie!

I appreciate your prompt response. My aim is to make use of the opportunities presented earlier, specifically by employing FlexSimPy to directly access data within a FlexSim file and execute a model from FlexSim. However, it appears that there is limited information accessible regarding the proper construction of the solution within Visual Studio, and the usage itself seems rather cumbersome. Based on my findings, the only commands available in FlexSimPy are as follows:

['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'evaluate', 'getParameter', 'getPerformanceMeasure', 'open', 'receive', 'reset', 'run', 'runToTime', 'send', 'setParameter', 'stop', 'time']

The only example I have found, based on the information provided on the GitHub page, is the 'getattribute' method when accessing the FlexSim folder. However, I expected there to be a 'gettable' method that would address my previous question. Could you please inform me if there are any available resources that can guide me through this?

Thank you once again for your assistance.


Best regards,

Nick

1 Like 1 ·
Julie Weller avatar image Julie Weller Nick commented ·

That makes sense! You can easily do that by using the getPerformanceMeasure command. If you open your FlexSim model, you can add a new performance measure value that is actually an array. So if you choose the value field and then add something similar to this into the custom code option:

Array data;
Table table = Table("GlobalTable1");
table.cloneTo(Table(data));
return data;

Then your value should be that GlobalTable1 in the form of an array.

Then from Python you can pull that array using the getPerformanceMeasure command.

Does that answer your question?

0 Likes 0 ·
Nick avatar image Nick commented ·

Hi Julie,

I apologize for the delay in getting back to you; Mondays can be a bit challenging. Thanks for your response. I understand that there is a workaround in FlexSimPy that involves creating an array to import data into Python, but it's not exactly what I'm looking for.


Moving forward, my plan is to utilize the export function and save tables as CSV files. This way, I can use Python for the desired analytics tasks downstream. However, if I were to make a wish list, I would prefer Python to directly query the tables in FlexSim, eliminating the need for the intermediate step. Although this might be a potential development opportunity in the future, avoiding the middle step would greatly simplify things for me. I wouldn't have to handle CSV files and maintain code for exporting them from the model.


Once again, thank you for your response!


Thanks,


Nick

1 Like 1 ·