以下为源代码:
from requests import get
from bs4 import BeautifulSoup
import pygal
import taichi as ti
ti.init(arch=ti.gpu)
@ti.func
def get_data(data):
name, rating, change = [], [], []
for i in data:
name.append(list(i)[4])
rating.append(list(i)[5])
change.append(list(i)[6])
return name, rating, change
@ti.kernel
def main():
bar = pygal.Bar()
response = get('https://www.tiobe.com/tiobe-index/')
soup = BeautifulSoup(response.text, 'html.parser')
data = soup.find('table', {'id': 'top20'}).find('tbody').find_all('tr')
# print(len(list(data)))
name, rating, change = get_data(data)
bar.title = 'TIOBE Index for January 2025'
bar.x_labels = name
bar.add('Rating', rating)
bar.add('Change', change)
bar.render_to_file('tiobe.svg')
# print(name,rating,change)
main()
输出如下:
[Taichi] version 1.7.2, llvm 15.0.1, commit 0131dce9, win, python 3.12.6
[W 01/13/25 18:09:42.086 32604] [cuda_driver.cpp:taichi::lang::CUDADriverBase::load_lib@36] nvcuda.dll lib not found.
[Taichi] Starting on arch=vulkan
D:\PycharmProjects\tiobe\tiobe.py:20: SyntaxWarning: Calling non-taichi function "pygal.Bar". Scope inside the function is not processed by the Taichi AST transformer. The function may not work as expected. Proceed with caution! Maybe you can consider turning it into a @ti.func?
bar = pygal.Bar()
Traceback (most recent call last):
File "D:\PycharmProjects\tiobe\tiobe.py", line 34, in <module>
main()
File "D:\Program\Python\Lib\site-packages\taichi\lang\kernel_impl.py", line 1117, in wrapped
raise type(e)("\n" + str(e)) from None
taichi.lang.exception.TaichiTypeError:
File "D:\PycharmProjects\tiobe\tiobe.py", line 20, in main:
bar = pygal.Bar()
^^^^^^^^^^^^^^^^^
Invalid constant scalar data type: <class 'pygal.graph.bar.Bar'>