Search code examples
pythonpygal

AttributeError: 'NoneType' object has no attribute 'startswith'


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


url = "https://api.github.com/search/repositories? 
q=language:python&sort=stars"
r = requests.get(url)

response_dict = r.json()

names,stars = [],[]
for repo in repo_dicts:
    names.append(repo["name"])
    stars.append(repo["stargazers_count"])

my_style = LS("333366", base_style=LCS)
chart = pygal.Bar(style=my_style, x_label_rotation=45, show_legend=False)
chart.title = "Most starred Python projects on GitHub"
chart.x_labels = names

chart.add(" ", stars)
chart.render_to_file("repo_visual.svg")

When running this code I get an AttributeError. I am trying to plot the python projects with the most stars onto a bar graph using the pygal module. The task is from Python crash course by Eric Matthes. I am cross-checking my code with his and I can't seem to find any problems

trace:

Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/generatingdata/python_repos.py", line 
51, in <module>
chart.render_to_file("x.svg")

Any help is greatly appreciated.


Solution

  • @SagarJhamb your code have two faults
    1.repo_dicts is not initialised
    2. defining my_style= LS("333366", base_style=LCS) the value should start with #333366 for learning more about custom style with pygal you can check out[pygal documentation][1][1]: http://www.pygal.org/en/stable/documentation/parametric_styles.html

    import requests
    import pygal
    from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
    
    
    url = "https://api.github.com/search/repositories?q=language:python&sort=stars"
    r = requests.get(url)
    
    response_dict = r.json()
    # initialise response dict
    repo_dict=response_dict['items']
    
    names,stars = [],[]
    for repo in repo_dict:
       names.append(repo["name"])
       stars.append(repo["stargazers_count"])
    # #333366 remove your error of NoneType object has no attribute startswith
    #It is rightformat of using custom style with pygal
    #It should starts with #
    my_style = LS("#333366", base_style=LCS)
    chart = pygal.Bar( style=my_style, x_label_rotation=45, show_legend=False)
    chart.title = "Most starred Python projects on GitHub"
    chart.x_labels = names
    chart.add("stars", stars)
    chart.render_to_file("repo_visual.svg")