Skip to content

gallery

gallery

Example script demonstrating trendify data products. Run with: python example_trendify_static.py Then run: trendify make static example_output example_results

make_gallery(output_dir: Path)

Generates the gallery

Source code in src/trendify/gallery.py
def make_gallery(output_dir: Path):
    """
    Generates the gallery
    """

    # Create output directory
    output_dir.mkdir(exist_ok=True, parents=True)
    data_dir = output_dir.joinpath('data')
    data_dir.mkdir(exist_ok=True, parents=True)
    trendify_dir=output_dir.joinpath('trendify')

    try:
        from importlib.resources import files
        from shutil import copy
        copy(
            str(files('trendify').joinpath('gallery.py').resolve()), 
            str(output_dir.joinpath('gallery.py')),
        )
    except Exception as e:
        print(e)

    # Generate all examples
    products = (
        generate_sine_wave_example() +
        generate_scatter_plot_example() +
        generate_histogram_example() +
        generate_table_example()
    )

    # Save to JSON file
    collection = DataProductCollection(elements=products)
    data_dir.joinpath('data_products.json').write_text(collection.model_dump_json())

    # Run trendify commands
    run_trendify_commands(
        input_dir=data_dir,
        output_dir=trendify_dir,
    )

run_trendify_commands

run_trendify_commands(input_dir: str | Path, output_dir: str | Path)

Runs trendify commands with proper OS handling

Parameters:

Name Type Description Default
input_dir str | Path

Input directory containing data products

required
output_dir str | Path

Output directory for sorted products and assets

required
Source code in src/trendify/gallery.py
def run_trendify_commands(input_dir: str | Path, output_dir: str | Path):
    """
    Runs trendify commands with proper OS handling

    Args:
        input_dir (str | Path): Input directory containing data products
        output_dir (str | Path): Output directory for sorted products and assets
    """
    # Convert to Path objects
    input_dir = Path(input_dir).resolve()
    output_dir = Path(output_dir).resolve()

    # Determine if we need shell=True based on OS
    use_shell = platform.system() == "Windows"

    commands = [
        ["trendify", "products-sort", "-i", str(input_dir), "-o", str(output_dir), "-n", "1"],
        ["trendify", "assets-make-static", str(output_dir), "-n", "1"]
    ]

    for cmd in commands:
        print(f"\nExecuting command: {' '.join(cmd)}")
        try:
            # Run command and capture output
            result = subprocess.run(
                cmd,
                shell=use_shell,
                check=True,
                text=True,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE
            )
            print("Command output:")
            print(result.stdout)

            # Print any stderr output if it exists
            if result.stderr:
                print("Warnings/Errors:")
                print(result.stderr)

        except subprocess.CalledProcessError as e:
            print(f"Error executing command: {' '.join(cmd)}")
            print(f"Exit code: {e.returncode}")
            print("Error output:")
            print(e.stderr)
            sys.exit(1)
        except Exception as e:
            print(f"Unexpected error executing command: {' '.join(cmd)}")
            print(f"Error: {str(e)}")
            sys.exit(1)