pythonvisualizationpygal

'NoneType' object has no attribute 'decode'


I practice writing some code to get top repositories of Python from GitHub and this is the error I see:

enter image description here

this is the code which causes above stated error:

import requests 
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS


path = 'https://api.github.com/search/repositories?q=language:python&sort=stars'

r = requests.get(path)

response_dict = r.json()

# Explore information about the repositories.
repo_dicts = response_dict['items']

names, plot_dicts = [], []
for repo_dict in repo_dicts:
    names.append(repo_dict['name'])
    plot_dict = {
        'value': repo_dict['stargazers_count'],
        'label': repo_dict['description'],
        'xlink': repo_dict['html_url'],
    }
    plot_dicts.append(plot_dict)

my_style = LS('#333366', base_style=LCS)
# Make visualization.
my_config = pygal.Config()
chart = pygal.Bar(my_config, style=my_style)


my_style = LS('#333366', base_style=LCS)
chart.title = 'Most-Starred Python Projects on GitHub'
chart.x_labels = names

chart.add('', plot_dicts)
chart.render_to_file('python_repos.svg')

Can you help me with this please. Thank you


Solution

  • There seems to be a None value somewhere where a string is expected, and the traceback seems to show it is the label value.

    Try to change this:

    plot_dict = {
        'value': repo_dict['stargazers_count'],
        'label': repo_dict['description'],
        'xlink': repo_dict['html_url'],
    }
    

    To this:

    plot_dict = {
        'value': repo_dict['stargazers_count'],
        'label': repo_dict['description'] or "",
        'xlink': repo_dict['html_url'],
    }