I would like to start Flask in debug/development mode using a single command.
Given
A fresh terminal, changing into the project directory, activating a virtual environment and running a custom Makefile:
> cd project
> activate myenv
(myenv) > make
Output
Debug mode is OFF. However, running the commands separately turns it ON (as expected):
(myenv) > set FLASK_APP=app.py
(myenv) > set FLASK_ENV=development
(myenv) > flask run
Output
Code
I've created the following Makefile, but when run, the debug mode does not turn on:
Makefile
all:
make env && \
make debug && \
flask run
env:
set FLASK_APP=app.py
debug:
set FLASK_ENV=development
How do I improve the Makefile to run Flask in debug mode?
Note: instructions vary slightly for each operating system; at the moment, I am testing this in a Windows command prompt.
While I still believe a Makefile is a more general approach on other systems, I settled with #user657267's recommendation to use a batch file on Windows:
Code
# start_flask.bat
:: set environment varibles (app and debug mode)
set FLASK_APP=app.py
set FLASK_ENV=development
flask run
pause
Demo
> start_flask.bat
Output
set FLASK_APP=app.py
set FLASK_ENV=development
flask run
* Serving Flask app "app.py" (lazy loading)
* Environment: development
* Debug mode: on
* Restarting with stat
* Debugger is active!
* ...
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
I am willing accept another solution.
Makefiles should be deterministic. Having one command which could toggle between the two is not the best way to do it.
Simply create your makefile like so:
FLASK_APP = app.py
FLASK := FLASK_APP=$(FLASK_APP) env/bin/flask
.PHONY: run
run:
FLASK_ENV=development $(FLASK) run
.PHONY: run-production
run-production:
FLASK_ENV=production $(FLASK) run
Now you can just do
make run
or
make run-production
Alternatively, on Linux inside of a makefile you can place everything on a single line without export keyword.
debug:
FLASK_APP=app.py FLASK_ENV=development flask run
As per flask docs.
Related
As my first batch script, I want to run my backend and frontend with just one script. My backend is in python with Django and my frontend is in react. I, therefore, have to processes I need to run. But I don't know how to run them in parallel as I need to start a virtual environment in python first.
I don't understand how it would log the server logs. Is there an option to make two terminal windows or a new terminal tab? Or would it log in to one terminal together?
#!/bin/bash
DJANGODIR=/Users/usr/Desktop/programming/app/django
REACTDIR=/Users/urs/Desktop/programming/app/react
# START REACT FRONTEND
echo Starting React Server
# Option 1
# cd $REACTDIR
# npm start
# Option 2
# start app from any directory without cd into it
# npm --prefix $REACTDIR run start
# START DJANGO BACKEND
echo Starting Django Server
# cd $DJANGODIR
# Start virtual environment
# source venv/bin/activate
# Start Server
# python3 manage.py runserver
# Activating and running in one command
# source venv/bin/activate python3 manage.py runserver
# Here is where I'm not knowing how to run them in parallel
source venv/bin/activate python3 manage.py runserver & npm --prefix $REACTDIR run start
Have you tried with the pipe operator? Pipe operator explained
I'm running a python script inside a docker container using crontab. Also, I set some environment variables (as database host, password, etc.) in .env file in the project's directory. If I run the script manually inside the container (python3 main.py) everything is working properly. But when the script is run by crontab the environment variables are not found (None).
I have the following setup:
Dockerfile
FROM ubuntu:latest
RUN apt-get update -y
RUN apt-get -y install cron
RUN apt-get install -y python3-pip python-dev
WORKDIR /home/me/theservice
COPY . .
RUN chmod 0644 theservice-cron
RUN touch /var/log/theservice-cron.log
RUN chmod +x run.sh
ENTRYPOINT ./run.sh
run.sh
#!/bin/bash
crontab theservice-cron
cron -f
docker-compose.yml
version: '3.7'
services:
theservice:
build: .
env_file:
- ./.env
theservice-cron
HOME=/home/me/theservice
* * * * * python3 /home/me/theservice/main.py >> /var/log/theservice-cron.log 2>&1
#* * * * * cd /home/me/theservice && python3 main.py >> /var/log/theservice-cron.log 2>&1
I assumed that the cronjob is running in another directory and there the environment variables set in /home/me/theservice/.env are not accessible. So I tried to add HOME=/home/me/theservice line in the theservice-cron file or just to execute /home/me/theservice before running the script but it didn't help.
In the python script, I use os to access environment variables
import os
print(os.environ['db_host'])
How I can fix this problem?
I had similar problem.
I did fix it using the following:
CMD printenv > /etc/environment && cron && tail -f /var/log/theservice-cron.log
According to
https://askubuntu.com/questions/700107/why-do-variables-set-in-my-etc-environment-show-up-in-my-cron-environment, cron reads env vars from /etc/enviroment
For those fighting to get ENV variables from docker-compose into docker, simply have a shell script run at ENTRYPOINT in your Dockerfile, with
printenv > /etc/environment
again, the naming of "/etc/environment" is CRUCIAL !
And then in your crontab, have it call a shell script:
* * * * * bash -c "sh /var/www/html/cron_php.sh"
The scripts simply does :
#!/bin/bash
cd /var/www/html
php whatever.php
You will now have the docker-compose environment variables in your php cron application. It took me a full day to figure this out. Hope i save someone's trouble now !
UPDATE:
In Azure Docker (Web app) the mechanism doesn't seem to work. A small tweak is needed:
In the Dockerfile, in the ENTRYPOINT sh script, write a file (and CHMOD to execution rights chmod 770 ) /etc/environments.sh using this command:
eval $(printenv | awk -F= '{print "export " $1"=""""$2""" }' >> /etc/environments.sh)
Then, in your crontab shell where you execute php, do this:
#!/bin/bash
. /etc/environments.sh
php whatever.php
Notice the "." instead of source. Even though the Docker container is Linux using bash, source did not do the trick, the . did work.
Note: In my local Windows Docker the first solution, using /etc/envrionment worked fine. I was baffled to find out that on Azure the second fix was needed.
I started a new project using Django. This project is build using Docker with few containers and poetry to install all dependencies.
When I first run docker-compose up -d, everything is installed correctly. Actually, this problem is not related with Docker I suppose.
After I run that command, I'm running docker-compose exec python make -f automation/local/Makefile which has this content
Makefile
.PHONY: all
all: install-deps run-migrations build-static-files create-superuser
.PHONY: build-static-files
build-static-files:
python manage.py collectstatic --noinput
.PHONY: create-superuser
create-superuser:
python manage.py createsuperuser --noinput --user=${DJANGO_SUPERUSER_USERNAME} --email=${DJANGO_SUPERUSER_USERNAME}#zitec.com
.PHONY: install-deps
install-deps: vendor
vendor: pyproject.toml $(wildcard poetry.lock)
poetry install --no-interaction --no-root
.PHONY: run-migrations
run-migrations:
python manage.py migrate --noinput
pyproject.toml
[tool.poetry]
name = "some-random-application-name"
version = "0.1.0"
description = ""
authors = ["xxx <xxx#xxx.com>"]
[tool.poetry.dependencies]
python = ">=3.6"
Django = "3.0.8"
docusign-esign = "^3.4.0"
[tool.poetry.dev-dependencies]
pytest = "^3.4"
django-debug-toolbar = "^2.2"
Debug toolbar is installed by adding those entries under settings.py (MIDDLEWARE / INSTALLED_APP) and even DEBUG_TOOLBAR_CONFIG with next value: SHOW_TOOLBAR_CALLBACK.
Let me confirm that EVERYTHING works after fresh docker-compose up -d. The problem occurs after I stop container and start it again using next commands:
docker-compose down
docker-compose up -d
When I try to access the project it says that Module debug_toolbar does not exist!.
I read all questions from this website, but nothing worked for me.
Has anyone encountered this problem before?
That sounds like normal behavior. A container has a temporary filesystem, and when the container exits any changes that have been made in that filesystem will be permanently lost. Deleting and recreating containers is extremely routine (even just changing environment: or ports: settings in the docker-compose.yml file would cause that to happen).
You should almost never install software in a running container. docker exec is an extremely useful debugging tool, but it shouldn't be the primary way you interact with your container. In both cases you're setting yourself up to lose work if you ever need to change a Docker-level setting or update the base image.
For this example, you can split the contents of that Makefile into two parts, the install_deps target (that installs Python packages but doesn't have any external dependencies) and the rest (that will depend on a database running). You need to run the installation part at image-build time, but the Dockerfile can't access a database, so the remainder needs to happen at container-startup time.
So in your image's Dockerfile, RUN the installation part:
RUN make install-reps
You will also need an entrypoint script that does the rest of the first-time setup, then runs the main container command. This can look like:
#!/bin/sh
make run-migrations build-static-files create-superuser
exec "$#"
Then run this in your Dockerfile:
COPY entrypoint.sh .
ENTRYPOINT ["./entrypoint.sh"]
CMD python3 manage.py runserver --host 0.0.0.0
(I've recently seen a lot of Dockerfiles that have just ENTRYPOINT ["python3"]. Splitting ENTRYPOINT and CMD this way isn't especially useful; just move the python3 interpreter command into CMD.)
This question already has answers here:
How to run a flask application?
(6 answers)
Closed 9 months ago.
i have installed flask and i am trying to run flask using the windows power shell
but i am unable to do so
PS C:\path\to\app> $env:FLASK_APP = "hello.py"
the format is given the documentation what should we type in $env?
p.s. I was able to run flask using command prompt using the code
first, you have to activate the virtual environment for your Flask app (i'm assuming you are using venv the default virtual environment module for python : py -m venv venv)
PS C:\myapps\flask\helloflask> .\venv\Scripts\activate
(venv) PS C:\myapps\flask\helloflask>
set the FLASK_APP environment variable:
(venv) PS C:\myapps\flask\helloflask> $env:FLASK_APP="helloflask:create_app('development')"
note the double quotes wrapping the app name "helloflask:create_app('development')" otherwise Power Shell triggers a red error.
refer to this doc on using app factory pattern and i would recommend you this good reads on How to Run a Flask Application
maybe you want to check if FLASK_APP has been set:
(venv) PS C:\myapps\flask\helloflask> $env:FLASK_APP
helloflask:create_app('development')
now you can run your Flask app:
(venv) PS C:\myapps\flask\helloflask> flask run
* Serving Flask app "helloflask:create_app('development')"
* Environment: development
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [23/Jun/2020 12:47:20] "←[37mGET / HTTP/1.1←[0m" 200 -
finally i recommend you using .flaskenv under the root of your project to store your Flask app related environment variables (FLASK_APP among others):
/.flaskenv
FLASK_APP=helloflask:create_app('development')
# FLASK_APP=helloflask:create_app('testing')
# FLASK_APP=helloflask:create_app('production')
FLASK_ENV=development
FLASK_DEBUG=0
# FLASK_RUN_EXTRA_FILES=
# FLASK_RUN_HOST=
# FLASK_RUN_PORT=8080
# FLASK_RUN_CERT=
# FLASK_RUN_KEY=
To take advantage of this option you have to install the python-dotenv package (have a look at this doc). To do so :
(venv) PS C:\mypps\flask\helloflask> pip install python-dotenv
I don't know if this will still be usefull but this is how I got mine to work.
In powershell type:
.venv/Scripts/activate and press Enter
In the comand line make sure you see something like: (.env) if so then type:
$env:FLASK_APP = "yourappname.py" and press Enter then type
$env:FLASK_ENV = "development" and finally type
flask run
I have a Flask app that I'd like to deploy and start using Ansible. I've already got my playbook setup to install all the dependencies needed but I'm having trouble getting Ansible to start my application. I've used command and shell both of which do startup the Flask app, but they block Ansible from exiting and Flask doesn't popup in it's own terminal window, which makes it difficult to visually debug and see what Flask is doing.
#Current playbook; both command and shell act the same way
tasks:
- command: python3 /home/user/Desktop/Flask/app.py
- shell: python3 /home/user/Desktop/Flask/app.py
Q: How can I have Ansible startup a Flask script in it's own terminal window?
If you have gnu screen installed on this system then you can use it to background tasks. I use this to run a shell script asynchronously as the deploy user so that I can log in later and see how it's doing:
- name: Invoke reset script
command: /usr/bin/screen -d -m sudo -u deploy /usr/local/bin/do-reset.sh -i -y reset
async: True
poll: 0
The -d -m parameters tell screen to start up in detached mode, and the async and poll settings tell Ansible to background the command and forget about it.