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

in #programming3 years ago (edited)

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:

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')

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.


That documentation link was extremely nice.

I had tried to learn beem without any documentation in the past.

I figured out how to query information, but I never figured out how to execute transactions.


Happy to help! Since this example did some reading data from Hive, I’m thinking the next one will write some data to Hive. So you can see how to set up the keys to make transactions.

So in the film you are making is it like giant snakes vs bees?

You lost me mate!

You lost me with the I don't know any coding jokes....
Mind Blown GIF-downsized.gif

Ahhh gotcha. Python is a very popular programming language.

Nice one, well you seem to do know what you doing!

Genius GIF-downsized_large.gif

Sometimes I know what I’m doing. Other times I furiously search the web and hope for finding good answers.

Thanks a lot for this tutorial. Unfortunately I'm already stuck lol. I have python, visual studio installed. I have imported beem but when I run the code above in visual studio it gives me a lot of errors lol.

Error: Assert Exception:api_itr != _registered_apis.end(): Could not find API follow_api Lost connection or internal error on node: (1/100)

Yes, I saw the same error. The API node beem tried to connect to doesn’t support the follow_api. Beem will retry other nodes until it finds one that works.

To avoid hitting this error, you can specify a list of which API nodes Beem should use.

Sorry, I had the same problem. How do we do that please?

You can ignore the error, and the code will still work. It seems like beem is smart enough to remember which API node is stable.

Or, you can force beem to use your favorite node like this:

HIVE = beem.Hive(node=[API_NODE])

Then try the get_follow_count() call again.

Hope this helps!

Thanks now I have another error lol 🤣. I really struggle with python :-)

What is the new error?

It tells me syntax error and shows c:. I think there is a problem with my path...

The syntax error should include a stack trace indicating which line of code it tripped up on.

Exactly it wrote error on line 1 and showed C: as the thing that was wrong. I must be doing something totally wrong lol.

Congratulations @learncode! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You received more than 50 upvotes.
Your next target is to reach 100 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out the last post from @hivebuzz:

Hive Power Up Day - June 1st 2021 - Hive Power Delegation
Support the HiveBuzz project. Vote for our proposal!

That's a great tutorial! Very clearly presented, and I like how you've used the get_follow_count() function to explain python dictionaries. Keep up the good work!

Posted Using LeoFinance Beta

Thanks for reading and thanks for your support!

I've tried to learn Python a few times without success. I'm hoping this time I'll be able to learn enough with your tutorials to get some data from the blockchain. Having help with a specific application should be easier. Here's hoping . . .

Posted Using LeoFinance Beta

Hi Gillian, glad you found me.

If you don’t mind my asking, where did you get hung up before?

I will try to cover those concepts here to cover the gaps.

where did you get hung up before

I can't remember the specifics. I just found it hard learning stuff when it wasn't applied to a problem that I actually wanted to solve.

Primarily I want to be able to download my data with dates and amounts etc of the various tokens in a csv friendly format to make filing taxes easier.

I'm assuming that's possible with python but I could be wrong. 😊

Posted Using LeoFinance Beta

Ah interesting! It should be possible to get such data for Hive-Engine tokens. Maybe this could make a good example for when I get to the Hive-Engine blog post.

Maybe this could make a good example for when I get to the Hive-Engine blog post.

Sounds great.

First I have to find time to do the first 2 lessons. I'm crazy busy at the moment.

Posted Using LeoFinance Beta

No problem at all. Take your time. The content will be here for a while. In fact it’s immutable and lives forever on the blockchain.

After installing Beem I saw this message on the terminal:

WARNING: The script beempy is installed in '/home/user/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location

Do you know what this means? I replicated the example that you provided with no issues but I don't know what that warning means.

Beem also has a command line executable you can call directly instead of via Python script. The warning is telling you if you try to invoke beempy from your terminal, the command won’t be found. You will need to append the path to your PATH environment variable, or use the full path.

Thank you for the response, I had already figured it out and appended the path to the PATH environment. I learned something about linux in the meantime :)

Awesome! I’m glad to hear of your success

Yay! 🤗
Your content has been boosted with Ecency Points, by
Use Ecency daily to boost your growth on platform!

Support Ecency
Vote for Proposal
Delegate HP and earn more

Is the command prompt thing the "terminal"?

When I paste "python3 -m pip install beem" I get the same result as I did in Lesson 1.

Am I supposed to put cmd in front of "python3 -m pip install beem".

It doesn't work either way. 😢

Posted via

On Windows, the file naming is slightly different.

Please instead try python -m pip install beem.

Yeah. I tried that too and got the same error as in Lesson 1, namely . . .


Posted Using LeoFinance Beta

Python isn’t found on your path.

That’s cool. It looks like Windows will fix it for you if you run ‘Python’ without arguments.

Sorry. I have no idea what this means

if you run ‘Python’ without arguments.


Posted Using LeoFinance Beta

Type in the word Python and hit enter.

When I do that I get this pop up for Microsoft Store


Posted Using LeoFinance Beta

If you post from you will earn extra STEM tokens.

Thanks for the tip!

I'll need to find out what beem does so I can either find a C# equivalent or make one.

It’s just a convenient wrapper for Hive RPC API calls.

Step 2: check!

>>> account = beem.account.Account('fireguardian')
>>> account.get_follow_count()
{'account': 'fireguardian', 'follower_count': 629, 'following_count': 66}

Thanks for your write up. I'm running into issues getting beem installed, hope you can help. This is on my Windows 10 system. Thanks.

  •  crypto_aes.c
    scrypt-1.2.1/libcperciva/crypto/crypto_aes.c(6): fatal error C1083: Cannot open include file: 'openssl/aes.h': No such file or directory
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe' failed with exit code 2
    [end of output]

    note: This error originates from a subprocess, and is likely not a problem with pip.
    ERROR: Failed building wheel for scrypt
    Failed to build scrypt
    ERROR: Could not build wheels for scrypt, which is required to install pyproject.toml-based projects*

Do you find a solution?

I have the same problem

Find one!

Just install openSSL

Thanks for sharing.



PIZZA Holders sent $PIZZA tips in this post's comments:
@cryptothesis(2/10) tipped @learncode (x1)

Join us in Discord!

How can I fetch other data like mana of an account?