Posts

Part 2: Coding on Hive With Python - Installing Modules and Fetching Some Data from Hive

avatar of @learncode
25
@learncode
·
0 views
·
2 min read

In Part 2 we will install beem, a Hive module for Python, and get our hands on some Hive blockchain data. Let's go!

This is the second post in the series about coding with Hive APIs and working with Hive data. If you want to run the sample code here, you will first need to install Python. A brief guide on how to do this is provided in Part 1.


Installing Beem Module to Interact with Hive

Now that you've installed Python, it's time to upgrade it by installing Hive modules. For now, all we need to install is Beem. Beem is an awesome Python module created by @holger80. It's the "Unofficial Python Library for HIVE and STEEM". Beem also has excellent documentation here: https://beem.readthedocs.io/

Since we're using Python3 we already have everything we need to install modules. Like in part 1 where we invoked the Python interpreter for the first time, now we can call the same executable to fetch Beem. You can do this by entering the following command into your terminal: python3 -m pip install beem. This will download *Beem and all of its dependencies (other modules that beem requires). On Windows the file name is slightly different and the command should instead be python -m pip install beem

The next step is to fire up the Python interpreter and execute a line: import beem. If all goes well and the module is successfully imported, Python will say nothing and wait for the next line of input.


Fetching Some Data from Hive

Let's keep this simple for now. Hive is a social blockchain! Just a couple lines of code are needed to ask for my follower counts. First, create a beem Account object. Then call the object's get_follow_count() function. This call will make a network request to a Hivemind API node, and return the API response to our code.

Copy-paste this into your Python interpreter terminal to try it out:

import beem 
account = beem.account.Account('learncode') 
account.get_follow_count() 

The Python interpreter output looks like this:

>>> import beem 
>>> account = beem.account.Account('learncode') 
>>> account.get_follow_count() 
{'account': 'learncode', 'following_count': 0, 'follower_count': 3} 

The return value of the get_follow_count() function is a dictionary. Dictionaries are Python's key-value data type. In this case, the dictionary has three keys (and three values). The response is self-explanatory: my account 'learncode' is following 0 other accounts and is followed by 3 other accounts.

Try it yourself: replace learncode with your own account name in the example.


Putting it All Together

Part 2 explained how to install Python modules using pip, and how to import modules using the import keyword. Then, we used the beem module to fetch some data from the Hive blockchain.


p.s. Thanks for reading. Please let me know if anything is unclear or incorrect. This is intended to be a living document to be improved and maintained over time.

p.p.s This post is brought to you by the Hive.Pizza team.