Skip to content

adding the ability to export a report definition to a notebook's built in resources #613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

edwardpcharles
Copy link

adding the ability of the function to output to a notebooks built in resources - Would love to see this feature!

@edwardpcharles
Copy link
Author

@microsoft-github-policy-service agree

@edwardpcharles edwardpcharles changed the title Update _generate_report.py adding the ability to export a report definition to a notebook's built in resources Apr 13, 2025
@m-kovalsky
Copy link
Collaborator

Why specifically the notebook builtin resources section? Generally I've been having functions save to a lakehouse (then can use the OneLake File Explorer or Azure Storage Explorer to view/download the file.

@edwardpcharles
Copy link
Author

@m-kovalsky I don't always want to have to attach a lakehouse to my notebook - I would much prefer to be able to save everything self-contained in the notebook. I run a lot of little semantic link scripts across my models and keep them in notebooks in a specific workspace.

In the case of this specific function - I want my team to be able to run a notebook and quickly download a report's definition, so they can grab 1 or 2 snippets of it and then basically dump the file in the recycling bin. However, if all the files end up on OneLake now every single one of those team members needs to also know how to interact with oneLake and not just run the script and work with the report definition file.

@m-kovalsky
Copy link
Collaborator

How about we just make a helper function that saves any given item to the notebook resources? Seems more scalable than adding this parameter into each function.

@m-kovalsky
Copy link
Collaborator

m-kovalsky commented May 11, 2025

@edwardpcharles can have a versatile function like this:

def save_as_file(file_path: str, content, file_type: Optional[str] = None):

    """
    Saves the content to a file in the specified format.

    Parameters
    ----------
    file_path : str
        The file path where the content will be saved.
        Example: "./builtin/myfolder/myfile.json" (will save to the Notebook resources)
    content : any
        The content to be saved.
    file_type : str, default=None
        The file type to save the content as. If None, it will be inferred from the file path.
    """

    import json
    from pathlib import Path
    import csv
    import sempy_labs._icons as icons

    file_path = Path(file_path)
    file_path.parent.mkdir(parents=True, exist_ok=True)

    if file_type is None:
        file_type = file_path.suffix.lower()

    if file_type == ".json":
        with open(file_path, "w") as f:
            json.dump(content, f, indent=4)
    elif file_type == ".txt":
        with open(file_path, "w") as f:
            f.write(str(content))
    elif file_type in [".csv", ".tsv"]:
        delimiter = "\t" if file_type == ".tsv" else ","
        with open(file_path, "w", newline="") as f:
            writer = csv.writer(f, delimiter=delimiter)
            writer.writerows(content)
    else:
        with open(file_path, "wb") as f:
            f.write(content)

    print(f"{icons.green_dot} The file has been saved to the following location: '{file_path}.")

@edwardpcharles
Copy link
Author

@m-kovalsky this would be great -- sorry for my delay in response work + life got in the way a bit!

@m-kovalsky
Copy link
Collaborator

@edwardpcharles please try out the function I shared and let me know if it is useful for you. If so, I can add it to the helper module. I tried to make it generic so it can save a variety of file types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants