taipy icon indicating copy to clipboard operation
taipy copied to clipboard

Creating pages through methods fails

Open AlexandreSajus opened this issue 1 year ago • 2 comments

This code which creates a page using a method:

from taipy import Gui
import taipy.gui.builder as tgb

def create_page():
    with tgb.Page() as page:
        tgb.html("p", "page")
    return page


class ProjectHub(Gui):
    def __init__(self):
        self.page_via_method = self.create_page()
        super().__init__(self.page_via_method)
        self.run(use_reloader=True, debug=True)

    def create_page(self):
        with tgb.Page() as page:
            tgb.html("p", "page")
        return page


if __name__ == "__main__":
    ProjectHub()

Fails with traceback:

Traceback (most recent call last):
  File "c:\Users\asaju\Desktop\demo-llm-chat\color_bars.py", line 24, in <module>
    ProjectHub()
  File "c:\Users\asaju\Desktop\demo-llm-chat\color_bars.py", line 14, in __init__
    super().__init__(self.page_via_method)
  File "C:\Users\asaju\Desktop\demo-llm-chat\venv\Lib\site-packages\taipy\gui\gui.py", line 348, in __init__
    self.add_page(name=Gui.__root_page_name, page=page)
  File "C:\Users\asaju\Desktop\demo-llm-chat\venv\Lib\site-packages\taipy\gui\gui.py", line 1432, in add_page
    self.__var_dir.add_frame(page._frame)
  File "C:\Users\asaju\Desktop\demo-llm-chat\venv\Lib\site-packages\taipy\gui\utils\_variable_directory.py", line 38, in add_frame
    imported_var_list = _get_imported_var(frame)
                        ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\asaju\Desktop\demo-llm-chat\venv\Lib\site-packages\taipy\gui\utils\get_imported_var.py", line 19, in _get_imported_var
    st = ast.parse(inspect.getsource(frame))
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<unknown>", line 1
    def create_page(self):
IndentationError: unexpected indent

AlexandreSajus avatar Dec 15 '23 09:12 AlexandreSajus

@AlexandreSajus assign this to me

pravintargaryen avatar Jan 03 '24 17:01 pravintargaryen

@pravintargaryen It requires a deep understanding of Taipy code. We need to handle it internally. I remove the assignment.

jrobinAV avatar Feb 09 '24 14:02 jrobinAV

I have not seen this syntax before. Can you let me know where you saw this one? Thanks!

dinhlongviolin1 avatar Mar 04 '24 07:03 dinhlongviolin1

Which part of the syntax is unclear? Unfortunately, this is not my code. A Discord user reported this issue.

AlexandreSajus avatar Mar 04 '24 10:03 AlexandreSajus

Gui object is not designed to be inherited. They can create an instance of the Gui instead of inheriting them.

dinhlongviolin1 avatar Mar 04 '24 10:03 dinhlongviolin1

Okay I understand. We can close this as non-planned then

AlexandreSajus avatar Mar 04 '24 11:03 AlexandreSajus

This does not work even if you don't inherit from the Gui. This code give the same error:

from taipy.gui import Gui
import taipy.gui.builder as tgb
import pandas as pd

class FoodDatabase:
    def __init__(self):
        self.food_df = pd.DataFrame({
            "Meal": ["Lunch", "Dinner", "Lunch", "Lunch"],
            "Category": ["Food", "Food", "Drink", "Food"],
            "Name": ["Burger", "Pizza", "Soda", "Salad"],
            "Calories": [300, 400, 150, 200],
        })

    def create_page(self):
        with tgb.Page() as page:
            tgb.table('{self.food_df}', filter=True)
            return page

if __name__ == '__main__':
    food_database = FoodDatabase()
    page = food_database.create_page()
    Gui(page).run(debug=True, use_reloader=True)

I don't think it is supposed to be supported in Taipy, but maybe it could be an improvement.

FlorianJacta avatar Mar 21 '24 08:03 FlorianJacta