Skip to main content

Lucide icons for FastHTML

Project description

fastlucide

A convenient FastHTML wrapper for the excellent Lucide icon library.

Installation

Install from pypi

$ pip install fastlucide

How to use

from fastlucide import *

SvgSprites

The most convenient way to use fastlucide is to create an SvgSprites class, passing in a short prefix to ensure IDs are unique:

ss = SvgSprites('l-')

Then add icons whereever you want to show them, by calling the ss object with the desired name. Any additional attrs are added to the SVG.

(NB: we use show here to cause the SVGs to render in the notebook/docs. This isn’t needed in a regular FastHTML app.)

from fasthtml.common import show,Div
show(ss('a-arrow-down'))
show(ss('accessibility'))

Finally, be sure to render ss itself, to add the sprite sheet to the DOM.

show(ss)
<style>.lucide-icon { stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }</style>

fastlucide.icons

If you dynamically add a new icon to the DOM after loading the page (e.g using an HTMX swap), then the SvgSprites element will not contain that icon, so it won’t display. One solution to this is to preinitialize using e.g SvgSprites(nms=['air-vent', 'apple']).

Alternatively, you can import any icon name (sentence-cased, with underscores instead of hyphens) from fastlucide.icons, which will create an icon-generating function and also adds the name to spritesheet, which is an SvgSprite singleton.

from fastlucide.icons import spritesheet, Air_vent, Apple, Circle, X

You can now call Air_vent and Apple as functions – be sure to also include spritesheet in the DOM. If you use the same icon multiple times, you can optionally store it in a variable.

vent = Air_vent(stroke='green')
show(vent, Apple(sz=48, fill='red'), vent,
     spritesheet)

<style>.lucide-icon { stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }</style>

You can create a combination of SVGs:

show(Circle(stroke='steelblue') + X(stroke='crimson'))
show(spritesheet)

<style>.lucide-icon { stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }</style>

Because fastlucide.icons supports the __dir__ protocol, you can use tab-completion in jupyter and similar environments to view a list of matching icons. Or call dir() programmatically.

from fastlucide import icons
[o for o in dir(icons) if o.startswith('Star')]
['Star', 'Star_half', 'Star_off']

How it works

When we render an icon, the actual SVG path information is not included – only an href to an id is there:

ss('a-arrow-down')
<svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" height="24px" width="24px" class="lucide-icon "><use href="#l-a-arrow-down"></use></svg>

The definitions of these ids is provided in the ss object itself, along with style information:

from fastcore.xml import highlight
from IPython.display import Markdown
Markdown(highlight(ss))
<style>.lucide-icon { stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }</style>
<svg xmlns="http://www.w3.org/2000/svg" style="display: none"><defs><symbol id="l-a-arrow-down"><path d="M3.5 13h6"></path><path d="m2 16 4.5-9 4.5 9"></path><path d="M18 7v9"></path><path d="m14 12 4 4 4-4"></path></symbol><symbol id="l-accessibility"><circle cx="16" cy="4" r="1"></circle><path d="m18 19 1-7-6 1"></path><path d="m5 8 3-3 5.5 3-2.36 3.5"></path><path d="M4.24 14.5a5 5 0 0 0 6.88 6"></path><path d="M13.76 17.5a5 5 0 0 0-6.88-6"></path></symbol></defs></svg>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastlucide-0.0.7.tar.gz (88.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastlucide-0.0.7-py3-none-any.whl (89.3 kB view details)

Uploaded Python 3

File details

Details for the file fastlucide-0.0.7.tar.gz.

File metadata

  • Download URL: fastlucide-0.0.7.tar.gz
  • Upload date:
  • Size: 88.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for fastlucide-0.0.7.tar.gz
Algorithm Hash digest
SHA256 2bab2d3fe05f0aec144318a174cfcc4ef9622118b1c668dbe27ec56f26d25788
MD5 e01bbceb73b87f1db6db53169b403077
BLAKE2b-256 047e8b66c0bbb8e69255ad2ef83229f971b6d98102b0cfffc3b1faed2eb0b68a

See more details on using hashes here.

File details

Details for the file fastlucide-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: fastlucide-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 89.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for fastlucide-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 e4f85008b4ca3735fab83583dcdbeab74cc45165a1018e3a1a5d907be55d9156
MD5 81ceea448d003350b11de521b0715dfe
BLAKE2b-256 72852688101b7130ee53962b7a0ddecd962b506f50c98516fb88a8b34ac4286e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page