Skip to content
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
2 changes: 2 additions & 0 deletions docs/customizing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ By default menu is constructed based on your classes and in a reversed navbar. L
category="Others", category_label=lazy_gettext('Other'), category_label='fa-envelope')
# Add a link
appbuilder.add_link("google", href="www.google.com", icon = "fa-google-plus")
# Add a link that opens in a new tab
appbuilder.add_link("Flask documentation", href="https://flask-appbuilder.readthedocs.io", icon="fa-book", target="_blank")

- Add separators::

Expand Down
18 changes: 18 additions & 0 deletions examples/quickexternalurl/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os

from flask import Flask
from flask_appbuilder import AppBuilder, SQLA

app = Flask(__name__)

basedir = os.path.abspath(os.path.dirname(__file__))
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join(basedir, "app.db")
app.config["CSRF_ENABLED"] = True
app.config["SECRET_KEY"] = "thisismyscretkey"

db = SQLA(app)
appbuilder = AppBuilder(app, db.session)

appbuilder.add_link("Flask documentation", href="https://flask-appbuilder.readthedocs.io", icon="fa-book", target="_blank")

app.run(host="0.0.0.0", port=8080, debug=True)

Check failure

Code scanning / CodeQL

Flask app is run in debug mode High

A Flask app appears to be run in debug mode. This may allow an attacker to run arbitrary code through the debugger.
7 changes: 7 additions & 0 deletions flask_appbuilder/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ def add_link(
category_label: str = "",
baseview: Optional["AbstractViewApi"] = None,
cond: Optional[Callable[..., bool]] = None,
target: str = "_self",
) -> None:
"""
Add your own links to menu using this method
Expand Down Expand Up @@ -501,6 +502,11 @@ def add_link(
then this link will be a part of the menu. Otherwise, it
will not be included in the menu items. Defaults to
:code:`None`, meaning the item will always be present.
:param target:
Target for the HTML <a> tag. This allows for links to
open in a new page or tab and even in seperate iframes.
Options are: :code:`_blank`, :code:`_self`, :code:`_parent`,
:code:`_top` or a custom iframe. Defaults to :code:`_self`
"""
if self.menu is None:
return
Expand All @@ -514,6 +520,7 @@ def add_link(
category_label=category_label,
baseview=baseview,
cond=cond,
target=target,
)
if self.app:
self._add_permissions_menu(name)
Expand Down
12 changes: 11 additions & 1 deletion flask_appbuilder/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class MenuItem(object):
def __init__(
self, name, href="", icon="", label="", childs=None, baseview=None, cond=None
self, name, href="", icon="", label="", childs=None, baseview=None, cond=None, target="_self"
):
self.name = name
self.href = href
Expand All @@ -19,10 +19,15 @@ def __init__(
self.childs = childs or []
self.baseview = baseview
self.cond = cond
self.target = target

def should_render(self) -> bool:
return bool(self.cond()) if self.cond is not None else True

def get_target(self) -> str:
"""Returns the target for a HTML <a> tag. Defaults to _self"""
return self.target

def get_url(self):
if not self.href:
if not self.baseview:
Expand Down Expand Up @@ -94,6 +99,7 @@ def get_data(self, menu=None):
"icon": item.icon,
"label": __(str(item.label)),
"url": item.get_url(),
"target": item.get_target(),
}
)
return ret_list
Expand Down Expand Up @@ -135,6 +141,7 @@ def add_link(
category_label="",
baseview=None,
cond=None,
target="_self",
):
label = label or name
category_label = category_label or category
Expand All @@ -147,6 +154,7 @@ def add_link(
label=label,
baseview=baseview,
cond=cond,
target=target,
)
)
else:
Expand All @@ -159,6 +167,7 @@ def add_link(
label=label,
baseview=baseview,
cond=cond,
target=target,
)
menu_item.childs.append(new_menu_item)
else:
Expand All @@ -172,6 +181,7 @@ def add_link(
label=label,
baseview=baseview,
cond=cond,
target=target,
)
self.find(category).childs.append(new_menu_item)

Expand Down
2 changes: 1 addition & 1 deletion flask_appbuilder/templates/appbuilder/navbar_menu.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% macro menu_item(item) %}
<a tabindex="-1" href="{{item.get_url()}}">
<a tabindex="-1" href="{{item.get_url()}}" target="{{item.get_target()}}">
{% if item.icon %}
<i class="fa fa-fw {{item.icon}}"></i>&nbsp;
{% endif %}
Expand Down
Loading