Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature view img #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/
19 changes: 19 additions & 0 deletions flask_nav/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ def active(self):
return request.path == self.get_url()


class ViewImg(View):
"""View with img instead of text.
The ``endpoint``, ``*args`` and ``**kwargs`` are passed on to
:func:`~flask.url_for` to get the link.
:param img_src: The source of img for the link.
:param img_alt: The alternate text of img for the link.
:param endpoint: The name of the view.
:param args: Extra arguments for :func:`~flask.url_for`
:param kwargs: Extra keyword arguments for :func:`~flask.url_for`
"""
def __init__(self, img_src, img_alt, endpoint, *args, **kwargs):
self.img_src = img_src
self.img_alt = img_alt
self.text = img_alt
self.endpoint = endpoint
self.url_for_args = args
self.url_for_kwargs = kwargs


class Separator(NavigationItem):
"""Separator.

Expand Down
10 changes: 10 additions & 0 deletions flask_nav/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ def visit_View(self, node):
title=node.text,
**kwargs)

def visit_ViewImg(self, node):
item = tags.li()
a = tags.a(href=node.get_url(), title=node.text)
a.add(tags.img(src=node.img_src, alt=node.img_alt))
item.add(a)
if node.active:
item['class'] = 'active'

return item

def visit_Subgroup(self, node):
group = tags.ul(_class='subgroup')
title = tags.span(node.title)
Expand Down