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

Convert from settings.py to an .env file #32

Open
picsldev opened this issue Apr 3, 2018 · 5 comments
Open

Convert from settings.py to an .env file #32

picsldev opened this issue Apr 3, 2018 · 5 comments
Assignees
Labels

Comments

@picsldev
Copy link
Contributor

picsldev commented Apr 3, 2018

pipenv reads the .env files automatically, loading the environment variables that we put into them.

It is a very powerful way to correctly configure an application, following the indications of "The Twelve-Factor App".

An example of a settings.py file converted to an .env file could be the following:

desarrollo@desarrollo:~/Proyectos/pipenv/blask$ cat .env

# Minimal conf for Blask
FLASK_APP=main.py

# Name of the Template Folder.
templateDir="templates"

# Name of the post Folder
postDir="posts"

# default name of the template file.
defaultLayout="template.html"

# Default name of the static Folder
staticDir="static"

# Title of the blog
tittle="Blask | A Simple Blog Engine Based on Flask"

All the environment variables that we want to define, can be included in the .env file.

.gitignore excludes the .env file so that it can not be versioned, so that we can store sensitive data inside it.

A very popular custom is to create a file .env.example, with sample data, so that other developers find it very easy to generate their own .env file from the .env.example file .

@zerasul zerasul added the good first issue label Apr 3, 2018
@zerasul
Copy link
Owner

zerasul commented Apr 3, 2018

This can be a Good issue. Use enviorement variables instead a py file. Of course, there is another issue for use it with a Dictionary using a json file (#20). So i think we can use enviorement variables too.

@JuanjoSalvador
Copy link
Contributor

JuanjoSalvador commented Apr 3, 2018

Seems fixed on #31

@JuanjoSalvador
Copy link
Contributor

JuanjoSalvador commented Oct 10, 2018

Hey, how can we read this settings (from .env file) to use them on Blask?

@mhered
Copy link

mhered commented Oct 3, 2021

Hola Victor creo que quizas podr'ia ayudar con esto, con un poco de guia por tu parte

@mhered
Copy link

mhered commented Oct 15, 2021

Hola Victor. Creo que tengo claro como hacerlo.
Entiendo que queremos que settings.py siga funcionando para tener retrocompatibilidad, pero ¿con que precedencia?

La opcion de definir la configuration en settings.py va a seguir funcionando y tendrá precedencia cuando se importe settings y se llame a BlaskApp con parámetros:

    from blask import BlaskApp
    import settings

    if __name__ == '__main__':
        b = BlaskApp(templateDir=settings.templateDir, postDir=settings.postDir
                  , defaultLayout=settings.defaultLayout,
              staticDir=settings.staticDir, title=settings.title, errors={404:'404'})
        b.run()

Sin embargo, si se define la variable de entorno BLASK_SETTINGS y tambien hay un .env, ¿a cual de las dos opciones doy prioridad? ¿Permito que se combinen variables de las dos fuentes? (ej. usar defaultLayout de settings.py y title de .env)

mhered added a commit to mhered/blask that referenced this issue Oct 17, 2021
Add support for .env file. Close Issue zerasul#32.

This Pull Request adds support to define blask settings using a .env
file.

**Usage**

Add an .env file in the root folder defining the variables FLASK_APP,
templateDir, postDir, defaultLayout, staticDir, title.
(Use .env.example as template)

`$ pipenv shell` will launch the virtualenv and automatically create
environment variables from .env

**Behaviour**

In order to ensure backwards compatibility, the following precedence is
considered to define blask settings:

1. if the environment variable BLASK_SETTINGS exists, the settings are
   loaded from this module (e.g. settings.py)
2. otherwise, environment variables, and finally default values are used
3. in any case, whenever Blask() is called with arguments, they always
   take precedence

**Description of changes**

* blask/blasksettings.py - in `BlaskSettings.__init__()``, replace
  default settings with environment variables. Add clarifications to
  docstring and comments.

* blask/blaskcli.py - fix typos in comments and docstrings, minor code
  formatting

* tests/settings_test.py - temporarily delete environment variables
  before the tests and restore them afterwards.
  Add test_from_dotenv() to test with environment variables

All tests passed.

Close zerasul#32.
mhered added a commit to mhered/blask that referenced this issue Oct 17, 2021
This Pull Request adds support to define blask settings using a .env
file.

**Usage**

Add an .env file in the root folder defining the variables FLASK_APP,
templateDir, postDir, defaultLayout, staticDir, title.
(Use .env.example as template)

`$ pipenv shell` will launch the virtualenv and automatically create
environment variables from .env

**Behaviour**

In order to ensure backwards compatibility, the following precedence is
considered to define blask settings:

1. if the environment variable BLASK_SETTINGS exists, the settings are
   loaded from this module (e.g. settings.py)
2. otherwise, environment variables, and finally default values are used
3. in any case, whenever Blask() is called with arguments, they always
   take precedence

**Description of changes**

* blask/blasksettings.py - in `BlaskSettings.__init__()``, replace
  default settings with environment variables. Add clarifications to
  docstring and comments.

* blask/blaskcli.py - fix typos in comments and docstrings, minor code
  formatting

* tests/settings_test.py - temporarily delete environment variables
  before the tests and restore them afterwards.
  Add test_from_dotenv() to test with environment variables

All tests passed.

Close zerasul#32.
zerasul added a commit that referenced this issue Oct 27, 2021
 mhered-#32-read settings from dotenv file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants