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

avatar
(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: 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.



0
0
0.000
54 comments
avatar

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!
0
0
0.000
avatar

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

0
0
0.000
avatar

Thanks for reading and thanks for your support!

0
0
0.000
avatar

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: https://api.hive.blog (1/100)

0
0
0.000
avatar

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.

0
0
0.000
avatar

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

0
0
0.000
avatar
(Edited)

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:

API_NODE = 'https://api.deathwing.me'
HIVE = beem.Hive(node=[API_NODE])
beem.instance.set_shared_blockchain_instance(HIVE)

Then try the get_follow_count() call again.

Hope this helps!

0
0
0.000
avatar
(Edited)

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

0
0
0.000
avatar

What is the new error?

0
0
0.000
avatar

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

0
0
0.000
avatar

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

0
0
0.000
avatar

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

0
0
0.000
avatar

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

0
0
0.000
avatar

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.

0
0
0.000
avatar

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

0
0
0.000
avatar

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.

0
0
0.000
avatar

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

0
0
0.000
avatar

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.

0
0
0.000
avatar

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.

!wine

0
0
0.000
avatar

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.

0
0
0.000
avatar

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.

0
0
0.000
avatar

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.

0
0
0.000
avatar

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

0
0
0.000
avatar

Awesome! I’m glad to hear of your success

0
0
0.000
avatar

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

0
0
0.000
avatar

You lost me mate!

0
0
0.000
avatar

You lost me with the title....lol bees...snakes....animals....no? I don't know any coding jokes....
Mind Blown GIF-downsized.gif

0
0
0.000
avatar

Ahhh gotcha. Python is a very popular programming language.

0
0
0.000
avatar

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

Genius GIF-downsized_large.gif

0
0
0.000
avatar

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

0
0
0.000
avatar

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 proofofbrain.io

0
0
0.000
avatar

On Windows, the file naming is slightly different.

Please instead try python -m pip install beem.

0
0
0.000
avatar

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

image.png

Posted Using LeoFinance Beta

0
0
0.000
avatar

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.

0
0
0.000
avatar

Sorry. I have no idea what this means

if you run ‘Python’ without arguments.

😂

Posted Using LeoFinance Beta

0
0
0.000
avatar

Type in the word Python and hit enter.

0
0
0.000
avatar

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

image.png!

Posted Using LeoFinance Beta

0
0
0.000
avatar

Yep. I think you can install Python from the Microsoft store and it will set up your path for you.

0
0
0.000
avatar

If you post from stemgeeks.net you will earn extra STEM tokens.

0
0
0.000
avatar

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

0
0
0.000
avatar

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

0
0
0.000
avatar

Step 2: check!

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

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*

0
0
0.000
avatar

Do you find a solution?

I have the same problem

0
0
0.000
avatar

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

0
0
0.000