pythonblockchain.info-api

Cant use get_chart function in blockchain python client


Trying to run

import blockchain
from blockchain import statistics

get_chart(chart_type="mempool-size", time_span="1year")

Gives a function not defined error, despite get_chart being defined in the statistics.py file for the blockchain.info client. How can I run the get_chart function?

Does anyone have any troubleshooting ideas? Question has been up for a couple days and I'm stuck. I've checked the GitHub repo for issues and can't find any, haven't tried anything else yet as I'm unsure where to start.

I'm happy with any python solution that can get chart data from https://blockchain.info


Solution

  • As you said, get_chart is defined in blockchain.statistics, but importing the statistics module does bring its members into the global namespace. You have to dot off of it to access its members, such as get_chart:

    from blockchain import statistics
    
    statistics.get_chart(chart_type="mempool-size", time_span="1year")
    

    Alternatively you can import the function directly:

    from blockchain.statistics import get_chart
    
    get_chart(chart_type="mempool-size", time_span="1year")
    

    Unfortunately that won't solve the larger issue at hand which is that the repository for the package appears to be abandoned. For your request it tries to access data from the URL https://blockchain.info/charts/mempool-size?format=json&timespan=1year, which results it in downloading an HTML page instead of JSON.

    Still, you can access the charts API using the docs provided here: https://www.blockchain.com/api/charts_api

    For your request the correct URL to use is: https://api.blockchain.info/charts/mempool-size?format=json&timespan=1year

    You can download it and parse the JSON into a dictionary like so:

    import json
    from urllib.request import urlopen
    
    url = 'https://api.blockchain.info/charts/mempool-size?format=json&timespan=1year'
    data = json.loads(urlopen(url).read())