SoFunction
Updated on 2024-11-21

Solved LightColorizedStyle parameter issue

The LightColorizedStyle parameter of the

concern

In the case of using the API in Python Programming: From Beginner to Practical, the imported LightColorizedStyle, passing in parameters like in the tutorial reports an error!

import requests
import pygal
from  import LightColorizedStyle as LCS, LightStyle as LS
# Execute API calls and store responses
url = '/search/repositories?q=language:python&sort=stars'
r = (url)
print("Status code:", r.status_code)
# Store the API response in a variable
response_dict = ()
print("Total repositories:", response_dict['total_count'])
# Explore warehouse information
response_dicts = response_dict['items']
# print("Repositories returned:", len(response_dicts))
names, stars = [], []
for response_dict in response_dicts:
    (response_dict['name'])
    (response_dict['stargazers_count'])
# Visualization
my_style = LS('#336699', base_style=LCS) #Mainly this is the wrong parameter for this line
chart = (style=my_style, x_label_rotation=45,show_legend=False)
 = 'Most-Starred Python Projects on GitHub'
chart.x_labels = names
('', stars)
chart.render_to_file('python_repos.svg')

The error message is shown in the figure

prescription

It may be because the package has been upgraded and the parameters are not the same, so enter the correct parameters

my_style = LS(colors=('#336699',), base_style=LCS)

Solutions

In pycharm ctrl+left mouse button (or ctrl+B) you can quickly locate the function, by this way clicking on LS, jumped to the class in the pygal package, you can see some properties as follows

class LightStyle(Style):
   """A light style"""
   background = 'white'
   plot_background = 'rgba(0, 0, 255, 0.1)'
   foreground = 'rgba(0, 0, 0, 0.7)'
   foreground_strong = 'rgba(0, 0, 0, 0.9)'
   foreground_subtle = 'rgba(0, 0, 0, 0.5)'
   colors = ('#242424', '#9f6767', '#92ac68',
             '#d0d293', '#9aacc3', '#bb77a4',
             '#77bbb5', '#777777')

By clicking on Style in this way, you can jump to the Style object and also see the colors property

Guessing to enter colors='#336699' like base_style=LCS, however after trying it still doesn't work

Look at points 1 and 2 again, see that colors is a tuple, guess you can't just enter a value, it's a tuple, so change it to colors=('#336699',), run it and it works!

Hereby documenting unprofessional troubleshooting solution ideas

The pygal tooltip is not working

Beginning python, follow "Python Programming from Beginning to Practice" follow the examples in the 17 chapters of the book

import requests
import pygal
from  import LightColorizedStyle as LCS, LightenStyle as LS
# Execute the API call and store the response, status_code=200 for success
url = '/search/repositories?q=language:python&sort=stars'
r = (url)
print("Status code:", r.status_code)
# Store the API response in a variable
response_dict = ()
# Outcomes
# print(response_dict.keys())
print("Total repositories:", response_dict['total_count'])
# Explore information about the warehouse
repo_dicts = response_dict['items']
# print("Repositories returned:", len(repo_dicts))
names, stars = [], []
for repo_dict in repo_dicts:
    (repo_dict['name'])
    (repo_dict['stargazers_count'])
# visualization, x_label_rotation means rotate the label 45° around the x-axis, show_legend=False means hide legend
my_style = LS('#333366', base_style=LCS)
my_config = ()
my_config.x_label_rotation = 45
my_config.show_legend = False
my_config.title_font_size = 24
my_config.label_font_size = 14
my_config.major_label_font_size = 18
my_config.truncate_label = 15
my_config.show_y_guides = False
my_config.width = 1000
chart = (my_config, style=my_style)
 = 'Most-Starred python Projects on GitHub'
chart.x_labels = names
('', stars)
chart.render_to_file('python_repos.svg')

The tools are used as follows:

  • python 3.8
  • pygal1.7
  • pycharm2020

from import LightenStyle as LS

report an error:cannot find referance LightenStyle

pip install pygal==2.4

No errors after updating to pygal2.4

However, the generated .svg file still fails to display the tooltip

After looking it up in Baidu, the reason could be that something seems to be happening between the script executed in python and the final rendering.

prescription

It may be necessary to change the python version, as the need for tooltips is not as strong at the moment, so I didn't bother.

Tossed a morning. The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.