okomarov / dash_on_flask Goto Github PK
View Code? Open in Web Editor NEWDash on Flask with login_required (and application factory pattern)
License: MIT License
Dash on Flask with login_required (and application factory pattern)
License: MIT License
After pip installs and setting up .envrc for dash multi page app. Im getting the error below.
flask db init
...
File "/dash_on_flask/app/__init__.py", line 33
url_base_pathname=f'/{base_pathname}/',
Hi,
many many thanks for your precious repo. I managed to set everything up in my local system. I'm now trying to dockerize it but I'm facing some issues with it.
Dockerfile :
FROM python:3.6
COPY . /app
WORKDIR /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
CMD [ "source .envrc" ]
CMD [ "flask run --host=0.0.0.0" ]
# The EXPOSE instruction indicates the ports on which a container # # will listen for connections
# Since Flask apps listen to port 5000 by default, we expose it
EXPOSE 5000
Build 👍 works fine :
docker build . -t flask_image
Run however fails:
docker run --name flask_container -p 81:5000 flask_image
>>docker: Error response from daemon: OCI runtime create failed: container_linux.go:344: starting container process caused "exec: \"flask run --host=0.0.0.0\": executable file not found in $PATH": unknown.
Any suggestions would be of great help!
Thanks,
Sébastien
First, I want to thank you for answering my email sent earlier today and thank you for this GitHub. It is a super helpful one to make a nice little app, the first one for me.
Anyway, today, my app is running, with a Flask app that I use to register and login users, and a Dash app that can be access after login in to see some plots. I recreated an app that have the same architecture than mine but with hello world dash graphs in it.
Here is a link to my GitHub Repo : https://github.com/mizujou/dashINflask
I would like to understand, find resources to learn about creating a new route, but this time from the Dash app to the Flask app.
Indeed, after login, I can access to the dash app using the navbar, so I have a route from Flask to Dash. But I haven't succeeded into making the same route but in the other way around.
I hope I was clear in my explanation.
Dear Oleg,
First of all congratulations on your awesome!
As many I've tried to access my flask_login username into my Dash applications. I've checked all the answers but I can't seem to make it work so I've started with your repo from scratch.
I am just trying to access on any of the dashapp files the username logged in with from flask_login import current_user but I'm only getting None answers.
Do you have any idea what I might change to get the username ?
Many thanks,
I would like to go automatically to the dashapp after login, but I'm struggling to make it work.
Can you give some help on what to change?
Hello wondering how you would go about making a nav bar for both you flask pages and you dash pages.
I have a nav bar in my dash app that uses some call backs for interaction.
possible thoughts are:
could i import a dash layout into a template?
could i {% extends "base.html" %} in a dash app tried in the index_string?
currently I'm just tring to make the same nav bar in both dash and in flask
Thanks for the great work.
(Using git clone -b feature/multiple_dash_apps https://github.com/okomarov/dash_on_flask.git)
I tried running from the feature/multiple_dash_apps but am getting:
jinja2.exceptions.TemplateNotFound: index.html
jinja2.exceptions.TemplateNotFound: login.html
Many thanks for any help
Hi and thanks for your template which I try to follow more or less here. But I can't figure out where to put db.create_all() or how to use it to create the tables in the database from the models.
I use a PostgreSQL database and tried putting it into app.__init__:register_extensions:
def register_extensions(server):
from app.extensions import db
from app.models import User
db.init_app(server)
with server.app_context():
# Create tables for our models.
db.create_all()
Unfortunately I get an OperationalError when doing this, even though I set my DATABASE_URL. I also tried with an adhoc sqlite:///mydb.db and didn't get an error, but it did seem to be empty (no tables) after running the app.
How did you create your database from the model in the example?
Or how would you do it using the commandline?
If I make the DATABASE_URI a Postgres database, launch the app server locally, and then navigate to the dashboard mounted at /dashboard
, I get the following error and the dashboard does not launch:
psycopg2.OperationalError: FATAL: too many connections for role "ulyvpgfa"
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/flask/app.py", line 2088, in __call__
return self.wsgi_app(environ, start_response)
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.handle_exception(e)
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/flask/app.py", line 2070, in wsgi_app
response = self.full_dispatch_request()
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/flask/app.py", line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/flask/app.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/flask/app.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/flask_login/utils.py", line 270, in decorated_view
elif not current_user.is_authenticated:
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/werkzeug/local.py", line 422, in __get__
obj = instance._get_current_object()
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/werkzeug/local.py", line 544, in _get_current_object
return self.__local() # type: ignore
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/flask_login/utils.py", line 26, in <lambda>
current_user = LocalProxy(lambda: _get_user())
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/flask_login/utils.py", line 346, in _get_user
current_app.login_manager._load_user()
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/flask_login/login_manager.py", line 318, in _load_user
user = self._user_callback(user_id)
File "/home/ubuntu/dash_on_flask/app/models.py", line 11, in load_user
return User.query.get(int(id))
File "<string>", line 2, in get
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/util/deprecations.py", line 390, in warned
return fn(*args, **kwargs)
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 943, in get
return self._get_impl(ident, loading.load_on_pk_identity)
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 947, in _get_impl
return self.session._get_impl(
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 2839, in _get_impl
return db_load_fn(
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/orm/loading.py", line 524, in load_on_pk_identity
session.execute(
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 1688, in execute
conn = self._connection_for_bind(bind)
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 1529, in _connection_for_bind
return self._transaction._connection_for_bind(
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 747, in _connection_for_bind
conn = bind.connect()
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 3166, in connect
return self._connection_cls(self, close_with_result=close_with_result)
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 96, in __init__
else engine.raw_connection()
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 3245, in raw_connection
return self._wrap_pool_connect(self.pool.connect, _connection)
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 3215, in _wrap_pool_connect
Connection._handle_dbapi_exception_noconnection(
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2069, in _handle_dbapi_exception_noconnection
util.raise_(
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 3212, in _wrap_pool_connect
return fn()
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 307, in connect
return _ConnectionFairy._checkout(self)
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 767, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 425, in checkout
rec = pool._do_get()
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/pool/impl.py", line 146, in _do_get
self._dec_overflow()
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
compat.raise_(
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/pool/impl.py", line 143, in _do_get
return self._create_connection()
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 253, in _create_connection
return _ConnectionRecord(self)
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 368, in __init__
self.__connect()
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 611, in __connect
pool.logger.debug("Error on connect(): %s", e)
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
compat.raise_(
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 605, in __connect
connection = pool._invoke_creator(self)
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/engine/create.py", line 578, in connect
return dialect.connect(*cargs, **cparams)
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 584, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/home/ubuntu/phenomic_scrna_embed/dashboard/venv/lib/python3.8/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: too many connections for role "ulyvpgfa"
(Background on this error at: https://sqlalche.me/e/14/e3q8)
Hello! I’m changing an existing Dash app for a covid-19 hackathon and am looking for ways to use flask i18n for the dash layout. Would it be in scope for this project to show how to do this?
follow along with the medium post you posted.
great implementation!
however, I don't know if it just me or anybody else is with me. once login, goes to /dashboard/, the page shows 'loading', and it stuck there.
any hint is appreciate.
I cannot seem to get CSS files to work. I have the static folder at the same level as templates. I am trying a simple example where I can the menu colors, for example. any advice?
Is there any way to integrate the dash app into one of the templates?
For example I may have some common elements such as a navbar
/ footer
that should also be displayed around the dash_app
. Any advice on how to do this or would we just have to duplicate that part of the HTML
code in the layout.py
?
Hi,
when trying to run flask db migrate -m 'init' I get the following error
Traceback (most recent call last):
File "C:\Users\Dipanjan\Anaconda\lib\runpy.py", line 193, in _run_module_as_main
"main", mod_spec)
File "C:\Users\Dipanjan\Anaconda\lib\runpy.py", line 85, in run_code
exec(code, run_globals)
File "C:\Users\Dipanjan\Desktop\dash_on_flask-master\flaskenv\Scripts\flask.exe_main.py", line 9, in
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\flask\cli.py", line 894, in main
cli.main(args=args, prog_name=name)
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\flask\cli.py", line 557, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\click\core.py", line 717, in main
rv = self.invoke(ctx)
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\click\core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\click\core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\click\core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\click\core.py", line 555, in invoke
return callback(*args, **kwargs)
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\click\decorators.py", line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\flask\cli.py", line 412, in decorator
return ctx.invoke(f, *args, **kwargs)
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\click\core.py", line 555, in invoke
return callback(*args, **kwargs)
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\flask_migrate\cli.py", line 90, in migrate
rev_id, x_arg)
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\flask_migrate_init.py", line 95, in wrapped
f(*args, **kwargs)
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\flask_migrate_init.py", line 215, in migrate
version_path=version_path, rev_id=rev_id)
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\alembic\command.py", line 197, in revision
script_directory.run_env()
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\alembic\script\base.py", line 475, in run_env
util.load_python_file(self.dir, "env.py")
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\alembic\util\pyfiles.py", line 90, in load_python_file
module = load_module_py(module_id, path)
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\alembic\util\compat.py", line 156, in load_module_py
spec.loader.exec_module(module)
File "", line 678, in exec_module
File "", line 219, in _call_with_frames_removed
File "migrations\env.py", line 26, in
current_app.config.get('SQLALCHEMY_DATABASE_URI'))
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\alembic\config.py", line 237, in set_main_option
self.set_section_option(self.config_ini_section, name, value)
File "c:\users\dipanjan\desktop\dash_on_flask-master\flaskenv\lib\site-packages\alembic\config.py", line 264, in set_section_option
self.file_config.set(section, name, value)
File "C:\Users\Dipanjan\Anaconda\lib\configparser.py", line 1192, in set
self._validate_value_types(option=option, value=value)
File "C:\Users\Dipanjan\Anaconda\lib\configparser.py", line 1177, in _validate_value_types
raise TypeError("option values must be strings")
TypeError: option values must be strings
I am using 'export DATABASE_URL=sqlite:///$PWD/app.db' in my .envrc.
Thanks so much for this great work!
I've got everything running perfectly, but one hurdle I am trying to overcome is how to pass the current user into the dash app. I realize it's instantiated through login_required(), and would generally use current_user.username
or store it in a session['username']
but can't figure out how to do it in this case. Any pointers for this?
Much thanks in advance!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.