Python Forum
Create Choices from .ods file columns
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create Choices from .ods file columns
#1
The code below works however it runs the function get_m_g() twice.
which takes you through the selection process a second time.
How do I get the return values of the get_m_g function and pass them to another function without going through the choices a second time?


def list_m_g():
    file = 'List_O_E.ods'
    df = pd.read_excel(file)
    m_list = df.columns
    return m_list

def get_m_g():
    m_g = inquirer.checkbox(
        message="\nChoose the m g to work on today:",
        choices=list_m_g(),
        validate=lambda result: len(result) > 0 and len(result) <= 2, 
        invalid_message="should be at least 1 m g and no more then 2",
        instruction="(\nselect up to 2 m g)",
    ).execute() 

   return(m_g)

def get_e():
    m_c = get_m_g()
Reply
#2
This code does nothing but define three functions. How can it run get_m_g() twice?
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
(Dec-27-2023, 08:01 AM)Gribouillis Wrote: This code does nothing but define three functions. How can it run get_m_g() twice?

Sorry I forgot to add the main() code.
Still getting used to posting these replies.
I got it figured out now.

Thank You
Reply
#4
I have no idea what you are trying to do. I see you have a similar post that "works well".

https://python-forum.io/thread-41339.html

The problem is that if you are using the inquirer package the code that "works well" doesn't run at all. Not even close.

https://python-inquirer.readthedocs.io/en/latest/

I looked at the documentation and checkbox() doesn't have "invalid_message" or "instruction" arguments. The validate lambda is not valid because the validate function is passed two arguments. checkbox() returns a list of the selected items and lists don't support execute().

If I fix all that, there is still the problem that you cannot pass a Pandas series as the list of choices to checkbox().
    m_list = df.columns  # columns is a series.  Is not a valid type to use as choices to checkbox.
    return m_list
And when I fix that, I still have this problem:
def get_e():
    m_c = get_m_g()
This function draws the checkboxes, but it throws away the results. m_c is local to the function get_e(), and only exists while the function executes. There is no way for you to get the user selections.

The example below fixes all the problems:
import inquirer
import pandas as pd


def validate(answers, current):
    """Can only check 1 or 2 boxes."""
    return 0 < len(current) < 3


def select_columns(columns, prompt="Select"):
    return inquirer.checkbox(message=prompt, choices=columns, validate=validate)


def main():
    df = pd.read_excel("data.xlsx")
    columns = select_columns(list(df.columns), "Choose columns to display")
    print(df[columns])


if __name__ == "__main__":
    main()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a table with different sizes of columns in MS word pepe 8 1,588 Dec-08-2023, 07:31 PM
Last Post: Pedroski55
  Recommended way to read/create PDF file? Winfried 3 2,904 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  Use PM4PY and create working file thomaskissas33 0 678 Nov-14-2023, 06:53 AM
Last Post: thomaskissas33
  Create csv file with 4 columns for process mining thomaskissas33 3 762 Nov-06-2023, 09:36 PM
Last Post: deanhystad
  create exe file for linux? korenron 2 985 Mar-22-2023, 01:42 PM
Last Post: korenron
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,541 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  my first file won't create itself MehHz2526 2 908 Nov-27-2022, 12:58 AM
Last Post: MehHz2526
  deleting columns in CSV file astral_travel 8 2,382 Nov-26-2022, 09:36 PM
Last Post: astral_travel
  Replace columns indexes reading a XSLX file Larry1888 2 997 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Create multiple/single csv file for each sql records mg24 6 1,416 Sep-29-2022, 08:06 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020