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 logo nav #125

Open
wants to merge 3 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/
15 changes: 11 additions & 4 deletions flask_bootstrap/nav.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def visit_Navbar(self, node):
node_id = self.id or sha1(str(id(node)).encode()).hexdigest()

root = tags.nav() if self.html5 else tags.div(role='navigation')
root['class'] = 'navbar navbar-default'
root['class'] = 'navbar navbar-inverse navbar-fixed-top'

cont = root.add(tags.div(_class='container-fluid'))

Expand All @@ -37,11 +37,18 @@ def visit_Navbar(self, node):
# title may also have a 'get_url()' method, in which case we render
# a brand-link
if node.title is not None:
if hasattr(node.title, 'img_src'):
title = tags.img(src=node.title.img_src,
alt=getattr(node.title, 'img_alt', ''))
else:
title = node.title.text
if hasattr(node.title, 'get_url'):
header.add(tags.a(node.title.text, _class='navbar-brand',
href=node.title.get_url()))
a = tags.a(_class='navbar-brand',
href=node.title.get_url())
else:
header.add(tags.span(node.title, _class='navbar-brand'))
a = tags.span(node.title, _class='navbar-brand')
a.add(title)
header.add(a)

bar = cont.add(tags.div(
_class='navbar-collapse collapse',
Expand Down
48 changes: 48 additions & 0 deletions sample_app_logo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Welcome to the Flask-Bootstrap sample application. This will give you a
# guided tour around creating an application using Flask-Bootstrap.
#
# To run this application yourself, please install its requirements first:
#
# $ pip install -r sample_app/requirements.txt
#
# Then, you can actually run the application.
#
# $ flask --app=sample_app dev
#
# Afterwards, point your browser to http://localhost:5000, then check out the
# source.

from flask import Flask
from flask_appconfig import AppConfig
from flask_bootstrap import Bootstrap

from .frontend import frontend
from .nav import nav


def create_app(configfile=None):
# We are using the "Application Factory"-pattern here, which is described
# in detail inside the Flask docs:
# http://flask.pocoo.org/docs/patterns/appfactories/

app = Flask(__name__)

# We use Flask-Appconfig here, but this is not a requirement
AppConfig(app)

# Install our Bootstrap extension
Bootstrap(app)

# Our application uses blueprints as well; these go well with the
# application factory. We already imported the blueprint, now we just need
# to register it:
app.register_blueprint(frontend)

# Because we're security-conscious developers, we also hard-code disabling
# the CDN support (this might become a default in later versions):
app.config['BOOTSTRAP_SERVE_LOCAL'] = True

# We initialize the navigation as well
nav.init_app(app)

return app
7 changes: 7 additions & 0 deletions sample_app_logo/default_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# The default_config module automatically gets imported by Appconfig, if it
# exists. See https://pypi.python.org/pypi/flask-appconfig for details.

# Note: Don't *ever* do this in a real app. A secret key should not have a
# default, rather the app should fail if it is missing. For the sample
# application, one is provided for convenience.
SECRET_KEY = 'devkey'
22 changes: 22 additions & 0 deletions sample_app_logo/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from flask_wtf import Form
from wtforms.fields import *
from wtforms.validators import Required, Email


class SignupForm(Form):
name = TextField(u'Your name', validators=[Required()])
password = TextField(u'Your favorite password', validators=[Required()])
email = TextField(u'Your email address', validators=[Email()])
birthday = DateField(u'Your birthday')

a_float = FloatField(u'A floating point number')
a_decimal = DecimalField(u'Another floating point number')
a_integer = IntegerField(u'An integer')

now = DateTimeField(u'Current time',
description='...for no particular reason')
sample_file = FileField(u'Your favorite file')
eula = BooleanField(u'I did not read the terms and conditions',
validators=[Required('You must agree to not agree!')])

submit = SubmitField(u'Signup')
68 changes: 68 additions & 0 deletions sample_app_logo/frontend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# This contains our frontend; since it is a bit messy to use the @app.route
# decorator style when using application factories, all of our routes are
# inside blueprints. This is the front-facing blueprint.
#
# You can find out more about blueprints at
# http://flask.pocoo.org/docs/blueprints/

from flask import Blueprint, render_template, flash, redirect, url_for
from flask_bootstrap import __version__ as FLASK_BOOTSTRAP_VERSION
from flask_nav.elements import Navbar, View, ViewImg, Subgroup, Link, Text, Separator
from markupsafe import escape

from .forms import SignupForm
from .nav import nav

frontend = Blueprint('frontend', __name__)

# We're adding a navbar as well through flask-navbar. In our example, the
# navbar has an usual amount of Link-Elements, more commonly you will have a
# lot more View instances.
nav.register_element('frontend_top', Navbar(
ViewImg('http://placehold.it/150x50&text=Logo', 'Flask-Bootstrap',
'.index'),
View('Home', '.index'),
View('Forms Example', '.example_form'),
View('Debug-Info', 'debug.debug_root'),
Subgroup(
'Docs',
Link('Flask-Bootstrap', 'http://pythonhosted.org/Flask-Bootstrap'),
Link('Flask-AppConfig', 'https://github.com/mbr/flask-appconfig'),
Link('Flask-Debug', 'https://github.com/mbr/flask-debug'),
Separator(),
Text('Bootstrap'),
Link('Getting started', 'http://getbootstrap.com/getting-started/'),
Link('CSS', 'http://getbootstrap.com/css/'),
Link('Components', 'http://getbootstrap.com/components/'),
Link('Javascript', 'http://getbootstrap.com/javascript/'),
Link('Customize', 'http://getbootstrap.com/customize/'),
),
Text('Using Flask-Bootstrap {}'.format(FLASK_BOOTSTRAP_VERSION)),
))


# Our index-page just shows a quick explanation. Check out the template
# "templates/index.html" documentation for more details.
@frontend.route('/')
def index():
return render_template('index.html')


# Shows a long signup form, demonstrating form rendering.
@frontend.route('/example-form/', methods=('GET', 'POST'))
def example_form():
form = SignupForm()

if form.validate_on_submit():
# We don't have anything fancy in our application, so we are just
# flashing a message when a user completes the form successfully.
#
# Note that the default flashed messages rendering allows HTML, so
# we need to escape things if we input user values:
flash('Hello, {}. You have successfully signed up'
.format(escape(form.name.data)))

# In a real application, you may wish to avoid this tedious redirect.
return redirect(url_for('.index'))

return render_template('signup.html', form=form)
7 changes: 7 additions & 0 deletions sample_app_logo/nav.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from flask_nav import Nav

# To keep things clean, we keep our Flask-Nav instance in here. We will define
# frontend-specific navbars in the respective frontend, but it is also possible
# to put share navigational items in here.

nav = Nav()
5 changes: 5 additions & 0 deletions sample_app_logo/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
flask-appconfig>=0.10
flask-bootstrap
flask-nav
flask-debug
flask-wtf
4 changes: 4 additions & 0 deletions sample_app_logo/static/html5shiv.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions sample_app_logo/static/logo-nav.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*!
* Start Bootstrap - Logo Nav HTML Template (http://startbootstrap.com)
* Code licensed under the Apache License v2.0.
* For details, see http://www.apache.org/licenses/LICENSE-2.0.
*/

body {
padding-top: 70px; /* Required padding for .navbar-fixed-top. Change if height of navigation changes. */
}

.navbar-fixed-top .nav {
padding: 15px 0;
}

.navbar-fixed-top .navbar-brand {
padding: 0 15px;
}

@media(min-width:768px) {
body {
padding-top: 100px; /* Required padding for .navbar-fixed-top. Change if height of navigation changes. */
}

.navbar-fixed-top .navbar-brand {
padding: 15px 0;
}
}
6 changes: 6 additions & 0 deletions sample_app_logo/static/respond.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions sample_app_logo/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{# This simple template derives from ``base.html``. See ``base.html`` for
more information about template inheritance. #}
{%- extends "logo-nav.html" %}

{# Loads some of the macros included with Flask-Bootstrap. We are using the
utils module here to automatically render Flask's flashed messages in a
bootstrap friendly manner #}
{% import "bootstrap/utils.html" as utils %}


{# Inside the ``content`` is where you should place most of your own stuff.
This will keep scripts at the page end and a navbar you add on later
intact. #}
{% block content %}
<div class="container">
{%- with messages = get_flashed_messages(with_categories=True) %}
{%- if messages %}
<div class="row">
<div class="col-md-12">
{{utils.flashed_messages(messages)}}
</div>
</div>
{%- endif %}
{%- endwith %}
<div class="jumbotron">
<h1>Welcome to Flask-Bootstrap</h1>
<p>This example application demonstrates some (but not all!) of the
features of <a href="http://pythonhosted.org/Flask-Bootstrap">
Flask-Bootstrap</a>.</p>
<p>
<a class="btn btn-lg btn-default" role="button"
href="http://pythonhosted.org/Flask-Bootstrap" >Show docs</a>
</p>
</div>
{%- endblock %}
44 changes: 44 additions & 0 deletions sample_app_logo/templates/logo-nav.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{# ``base.html`` is the template all our other templates derive from. While
Flask-Bootstrap ships with its own base, it is good form to create a custom
one for our app, as it allows customizing some aspects.

Deriving from bootstap/base.html gives us a basic page scaffoling.

You can find additional information about template inheritance at

http://jinja.pocoo.org/docs/templates/#template-inheritance
#}
{%- extends "bootstrap/base.html" %}

{# We also set a default title, usually because we might forget to set one.
In our sample app, we will most likely just opt not to change it #}
{% block title %}Sample App for Flask-Bootstrap{% endblock %}

{# While we are at it, we also enable fixes for legacy browsers. First we
import the necessary macros: #}
{% import "bootstrap/fixes.html" as fixes %}

{# Then, inside the head block, we apply these. To not replace the header,
``super()`` is used: #}
{% block head %}
{{super()}}

{#- Docs: http://pythonhosted.org/Flask-Bootstrap/macros.html#fixes
The sample application already contains the required static files. #}
{{fixes.ie8()}}
{%- endblock %}

{# Adding our own CSS files is also done here. Check the documentation at
http://pythonhosted.org/Flask-Bootstrap/basic-usage.html#available-blocks
for an overview. #}
{% block styles -%}
{{super()}} {# do not forget to call super or Bootstrap's own stylesheets
will disappear! #}
<link rel="stylesheet" type="text/css"
href="{{url_for('static', filename='logo-nav.css')}}">
{% endblock %}

{# Finally, round things out with navigation #}
{% block navbar %}
{{nav.frontend_top.render()}}
{% endblock %}
15 changes: 15 additions & 0 deletions sample_app_logo/templates/signup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% import "bootstrap/wtf.html" as wtf %}

{%- extends "logo-nav.html" %}


{% block content %}
<div class="container">
<div class="jumbotron">
<h1>Signup for our awesome service</h1>
<p>Note: Your data isn't going anywhere.</p>
</div>
<div class="col-md-12">
{{form|render_form()}}
</div>
{%- endblock %}