Skip to content

Commit c98d8ff

Browse files
committed
Subnets price
1 parent 70138f3 commit c98d8ff

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

bittensor_cli/cli.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4470,7 +4470,7 @@ def subnets_list(
44704470
[green]$[/green] btcli subnets list
44714471
"""
44724472
if json_output and live_mode:
4473-
print_error("Cannot use --json-output and --live at the same time.")
4473+
print_error("Cannot use `--json-output` and `--live` at the same time.")
44744474
return
44754475
self.verbosity_handler(quiet, verbose, json_output)
44764476
subtensor = self.initialize_chain(network)
@@ -4515,6 +4515,9 @@ def subnets_price(
45154515
help="Show the price in log scale.",
45164516
),
45174517
html_output: bool = Options.html_output,
4518+
quiet: bool = Options.quiet,
4519+
verbose: bool = Options.verbose,
4520+
json_output: bool = Options.json_output,
45184521
):
45194522
"""
45204523
Shows the historical price of a subnet for the past 24 hours.
@@ -4532,6 +4535,10 @@ def subnets_price(
45324535
[green]$[/green] btcli subnets price --all --html
45334536
[green]$[/green] btcli subnets price --netuids 1,2,3,4 --html
45344537
"""
4538+
if json_output and html_output:
4539+
print_error("Cannot specify both `--json-output` and `--html`")
4540+
return
4541+
self.verbosity_handler(quiet=quiet, verbose=verbose, json_output=json_output)
45354542
if netuids:
45364543
netuids = parse_to_list(
45374544
netuids,
@@ -4540,23 +4547,23 @@ def subnets_price(
45404547
)
45414548
if all_netuids and netuids:
45424549
print_error("Cannot specify both --netuid and --all-netuids")
4543-
raise typer.Exit()
4550+
return
45444551

45454552
if not netuids and not all_netuids:
45464553
netuids = Prompt.ask(
4547-
"Enter the [blue]netuid(s)[/blue] to view the price of in comma-separated format [dim](or Press Enter to view all subnets)[/dim]",
4554+
"Enter the [blue]netuid(s)[/blue] to view the price of in comma-separated format [dim]"
4555+
"(or Press Enter to view all subnets)[/dim]",
45484556
)
45494557
if not netuids:
45504558
all_netuids = True
4551-
html_output = True
45524559
else:
45534560
netuids = parse_to_list(
45544561
netuids,
45554562
int,
45564563
"Netuids must be a comma-separated list of ints, e.g., `--netuids 1,2,3,4`.",
45574564
)
45584565

4559-
if all_netuids:
4566+
if all_netuids and not json_output:
45604567
html_output = True
45614568

45624569
if html_output and is_linux():
@@ -4570,6 +4577,7 @@ def subnets_price(
45704577
interval_hours,
45714578
html_output,
45724579
log_scale,
4580+
json_output,
45734581
)
45744582
)
45754583

bittensor_cli/src/commands/subnets/price.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
err_console,
1414
get_subnet_name,
1515
print_error,
16+
json_console,
1617
)
1718

1819
if TYPE_CHECKING:
@@ -26,6 +27,7 @@ async def price(
2627
interval_hours: int = 24,
2728
html_output: bool = False,
2829
log_scale: bool = False,
30+
json_output: bool = False,
2931
):
3032
"""
3133
Fetch historical price data for subnets and display it in a chart.
@@ -60,7 +62,7 @@ async def price(
6062
all_subnet_infos = await asyncio.gather(*subnet_info_cors)
6163

6264
subnet_data = _process_subnet_data(
63-
block_numbers, all_subnet_infos, netuids, all_netuids, interval_hours
65+
block_numbers, all_subnet_infos, netuids, all_netuids
6466
)
6567

6668
if not subnet_data:
@@ -71,17 +73,13 @@ async def price(
7173
await _generate_html_output(
7274
subnet_data, block_numbers, interval_hours, log_scale
7375
)
76+
elif json_output:
77+
json_console.print(json.dumps(_generate_json_output(subnet_data)))
7478
else:
7579
_generate_cli_output(subnet_data, block_numbers, interval_hours, log_scale)
7680

7781

78-
def _process_subnet_data(
79-
block_numbers,
80-
all_subnet_infos,
81-
netuids,
82-
all_netuids,
83-
interval_hours,
84-
):
82+
def _process_subnet_data(block_numbers, all_subnet_infos, netuids, all_netuids):
8583
"""
8684
Process subnet data into a structured format for price analysis.
8785
"""
@@ -772,6 +770,10 @@ async def _generate_html_output(
772770
print_error(f"Error generating price chart: {e}")
773771

774772

773+
def _generate_json_output(subnet_data):
774+
return {netuid: data for netuid, data in subnet_data.items()}
775+
776+
775777
def _generate_cli_output(subnet_data, block_numbers, interval_hours, log_scale):
776778
"""
777779
Render the price data in a textual CLI style with plotille ASCII charts.
@@ -802,7 +804,9 @@ def color_label(text):
802804

803805
fig.plot(
804806
block_numbers,
805-
data["prices"],
807+
data[
808+
"prices"
809+
], # TODO should this use `prices` instead of `data["prices"]`?
806810
label=f"Subnet {netuid} Price",
807811
interp="linear",
808812
lc="bae98f",

0 commit comments

Comments
 (0)