Skip to content

Commit 4a2913f

Browse files
committed
Add instructions to generate a secret key
1 parent 5b68f41 commit 4a2913f

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

code-image-generator/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,30 @@ Finally, run the Flask development server
3939
```
4040

4141
Now you can navigate to the address that's shown in the output when you start the server. Commonly, that's `http://localhost:5000/`.
42+
43+
## Secret Key
44+
45+
If you want to deploy your Flask app later, then it's a good idea to generate a proper secret key.
46+
47+
If you need to create cryptographically sound data like a Flask secret key, then you can use Python's [`secrets`](https://docs.python.org/3/library/secrets.html) module:
48+
49+
```pycon
50+
>>> import secrets
51+
>>> secrets.token_hex()
52+
'2e9ac41b1e0b66a8d93d66400e2300c4b4c2953f'
53+
```
54+
55+
The `.token_hex()` method returns a [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) string containing random numbers and letters from `0` to `9` and `a` to `f`. Use the value that `secrets.token_hex()` outputs for you and add it in your Flask project's `app.py` file:
56+
57+
```python hl_lines="6"
58+
# app.py
59+
60+
from flask import Flask, render_template
61+
62+
app = Flask(__name__)
63+
app.secret_key = "2e9ac41b1e0b66a8d93d66400e2300c4b4c2953f"
64+
65+
# ...
66+
```
67+
68+
To not save the secret key directly in your code, it may be a good idea to work with [environment variables](https://12factor.net/config). You can learn more about it in the Flask documentation about [Configuration Handling](https://flask.palletsprojects.com/en/2.3.x/config/).

code-image-generator/source_code_final/app.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
from pygments.formatters import HtmlFormatter
66
from pygments.lexers import Python3Lexer
77
from pygments.styles import get_all_styles
8-
98
from utils import take_screenshot_from_url
109

1110
app = Flask(__name__)
12-
app.secret_key = "mysecretkey"
11+
app.secret_key = (
12+
"AddYourSecretKeyHere" # See the README.md file for instructions
13+
)
1314

1415
PLACEHOLDER_CODE = "print('Hello, World!')"
1516
DEFAULT_STYLE = "monokai"

code-image-generator/source_code_step_02/app.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
)
99

1010
app = Flask(__name__)
11-
app.secret_key = "mysecretkey"
11+
app.secret_key = (
12+
"AddYourSecretKeyHere" # See the README.md file for instructions
13+
)
1214

1315
PLACEHOLDER_CODE = "print('Hello, World!')"
1416

code-image-generator/source_code_step_03/app.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
from pygments.styles import get_all_styles
1313

1414
app = Flask(__name__)
15-
app.secret_key = "mysecretkey"
15+
app.secret_key = (
16+
"AddYourSecretKeyHere" # See the README.md file for instructions
17+
)
1618

1719
PLACEHOLDER_CODE = "print('Hello, World!')"
1820
DEFAULT_STYLE = "monokai"

code-image-generator/source_code_step_04/app.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
from pygments.formatters import HtmlFormatter
1212
from pygments.lexers import Python3Lexer
1313
from pygments.styles import get_all_styles
14-
1514
from utils import take_screenshot_from_url
1615

1716
app = Flask(__name__)
18-
app.secret_key = "mysecretkey"
17+
app.secret_key = (
18+
"AddYourSecretKeyHere" # See the README.md file for instructions
19+
)
1920

2021
PLACEHOLDER_CODE = "print('Hello, World!')"
2122
DEFAULT_STYLE = "monokai"

0 commit comments

Comments
 (0)