Skip to content

Conversation

@leonace924
Copy link
Contributor

@leonace924 leonace924 commented Dec 23, 2025

Summary

This PR introduces a new create_table() utility function that eliminates code duplication and ensures consistent table styling throughout the CLI. The function reduces repetitive table configuration code from 11+ lines down to 1-4 lines while maintaining full flexibility.

Changes

New Utility Function

Added create_table() to bittensor_cli/src/bittensor/utils.py:

def create_table(*columns, title: str = "", **overrides) -> Table:
    """Creates a Rich Table with consistent CLI styling."""
    defaults = {
        "title": title,
        "show_footer": True,
        "show_edge": False,
        "header_style": "bold white",
        "border_style": "bright_black",
        "style": "bold",
        "title_justify": "center",
        "show_lines": False,
        "pad_edge": True,
    }
    config = {**defaults, **overrides}
    return Table(*columns, **config)

Refactored Locations

Updated three table creation sites in bittensor_cli/src/commands/subnets/subnets.py:

  1. Line 366 - subnets_list() function
  2. Line 1895 - Subnet registration confirmation table
  3. Line 2482 - Identity table with Column objects

Examples

Pattern 1: Simple Table (Add Columns Later)

Before (11 lines):

table = Table(
    title="My Subnets",
    show_footer=True,
    show_edge=False,
    header_style="bold white",
    border_style="bright_black",
    style="bold",
    title_justify="center",
    show_lines=False,
    pad_edge=True,
)
table.add_column("Netuid", justify="center")

After (2 lines):

table = create_table(title="My Subnets")
table.add_column("Netuid", justify="center")

Pattern 2: Table with Column Objects

Before (17 lines):

table = Table(
    Column(
        "Item",
        justify="right",
        style=COLOR_PALETTE["GENERAL"]["SUBHEADING_MAIN"],
        no_wrap=True,
    ),
    Column("Value", style=COLOR_PALETTE["GENERAL"]["SUBHEADING"]),
    title=f"\n[{COLOR_PALETTE['GENERAL']['HEADER']}]Settings\n",
    show_footer=True,
    show_edge=False,
    header_style="bold white",
    border_style="bright_black",
    style="bold",
    title_justify="center",
    show_lines=False,
    pad_edge=True,
)

After (8 lines):

table = create_table(
    Column(
        "Item",
        justify="right",
        style=COLOR_PALETTE["GENERAL"]["SUBHEADING_MAIN"],
        no_wrap=True,
    ),
    Column("Value", style=COLOR_PALETTE["GENERAL"]["SUBHEADING"]),
    title=f"\n[{COLOR_PALETTE['GENERAL']['HEADER']}]Settings\n",
)

Pattern 3: Custom Overrides

# Easy to override specific parameters
table = create_table(
    title="Custom Table",
    show_footer=False,
    border_style="blue"
)

# Advanced usage with rich box styles
from rich import box
table = create_table(
    title="Advanced",
    box=box.ROUNDED,
    expand=True,
    show_lines=True
)

Visual Examples

Here's how the tables look when rendered:

📊 Visual Examples of create_table() Output

======================================================================

1️⃣  Simple Subnets Table (Pattern 1)
----------------------------------------------------------------------
            My Subnets
 Netuid ┃ Name          ┃ Emission
━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━
   1    │ Alpha Network │    100.5
   2    │ Beta Network  │     85.3
   3    │ Gamma Network │     92.7
────────┼───────────────┼──────────

2️⃣  Identity Table (Pattern 2 - Column Objects)
----------------------------------------------------------------------
        Subnet Identity
 Property ┃ Value
━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━
     Name │ Alpha Network
    Owner │ 5GTestAddress123...
   Symbol │ α
    Price │ 0.0025 τ
──────────┼─────────────────────

3️⃣  Registration Confirmation
----------------------------------------------------------------------
          Register to netuid: 1
 Netuid ┃ Symbol ┃ Cost (τ) ┃ Hotkey
━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━
   1    │   α    │ τ 0.5000 │ 5GTjidas...
────────┼────────┼──────────┼─────────────

4️⃣  Custom Styled Table (with overrides)
----------------------------------------------------------------------
⚠️  Custom Border Style
 Command  ║   Status
══════════╬════════════
 register ║ ✓ Complete
 list     ║ ✓ Complete
 transfer ║ ⏳ Pending

✨ All tables use create_table() with consistent defaults!

Manual Verification

Tested actual usage in the CLI commands:

# Test subnets list command
$ btcli subnets list --network test
# ✓ Table renders correctly with new create_table()

# Test subnet registration
$ btcli subnets register --netuid 1 --network test
# ✓ Confirmation table displays properly

# Test subnet identity
$ btcli subnets identity --netuid 1
# ✓ Identity table with Column objects works

Breaking Changes

None.

Files Changed

  • bittensor_cli/src/bittensor/utils.py - Added create_table() function
  • bittensor_cli/src/commands/subnets/subnets.py - Refactored 3 table creations
  • bittensor_cli/src/commands/stake/
  • bittensor_cli/src/commands/liquidity/
  • bittensor_cli/src/commands/crowd/

Contribution by Gittensor, learn more at https://gittensor.io/

@leonace924
Copy link
Contributor Author

leonace924 commented Dec 23, 2025

@thewhaleking please review this reusable table PR
Thank you

@leonace924
Copy link
Contributor Author

Hi @ibraheem-abe, this is another util from my end after success and error consistency.
Hope you review today when you have a sec.
Thank you.

@ibraheem-abe
Copy link
Contributor

Thank you for your contribution

This is a good idea - I would like to review this one in depth.

Each existing table has been a result of multiple iterations and ideally we would like them to stay the same visually.

Also I dont think we need these tests for the utility if the implementation is solid.

@leonace924
Copy link
Contributor Author

@ibraheem-abe I removed the testing as it's not necessary
I've checked the every table visually, and confirmed that before and after are the same, util has default settings and override options.
So if any table doesn't have option inside default settings, we just pass that as override

@ibraheem-abe ibraheem-abe requested review from ibraheem-abe and thewhaleking and removed request for thewhaleking December 24, 2025 00:48
@ibraheem-abe
Copy link
Contributor

Would also like @thewhaleking's eye on this

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