Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Practice5
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CloudComputingCourse
Practice5
Commits
5695bf39
Commit
5695bf39
authored
1 month ago
by
jakovits
Browse files
Options
Downloads
Patches
Plain Diff
Example code for exercise 5.1
parent
74397dbc
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
app.py
+9
-3
9 additions, 3 deletions
app.py
templates/home.html
+6
-5
6 additions, 5 deletions
templates/home.html
with
15 additions
and
8 deletions
app.py
+
9
−
3
View file @
5695bf39
...
@@ -5,17 +5,19 @@ from flask import Flask, render_template, request
...
@@ -5,17 +5,19 @@ from flask import Flask, render_template, request
app
=
Flask
(
__name__
)
app
=
Flask
(
__name__
)
UPLOAD_FOLDER
=
'
./static/images
'
def
read_messages_from_file
():
def
read_messages_from_file
():
"""
Read all messages from a JSON file
"""
"""
Read all messages from a JSON file
"""
with
open
(
'
data.json
'
)
as
messages_file
:
with
open
(
'
data.json
'
)
as
messages_file
:
return
json
.
load
(
messages_file
)
return
json
.
load
(
messages_file
)
def
append_message_to_file
(
content
):
def
append_message_to_file
(
img_path
,
content
):
"""
Read the contents of JSON file, add this message to it
'
s contents, then write it back to disk.
"""
"""
Read the contents of JSON file, add this message to it
'
s contents, then write it back to disk.
"""
data
=
read_messages_from_file
()
data
=
read_messages_from_file
()
new_message
=
{
new_message
=
{
'
content
'
:
content
,
'
content
'
:
content
,
'
img_path
'
:
img_path
,
'
timestamp
'
:
datetime
.
now
().
isoformat
(
"
"
,
"
seconds
"
)
'
timestamp
'
:
datetime
.
now
().
isoformat
(
"
"
,
"
seconds
"
)
}
}
...
@@ -29,10 +31,14 @@ def append_message_to_file(content):
...
@@ -29,10 +31,14 @@ def append_message_to_file(content):
# The Flask route, defining the main behaviour of the webserver:
# The Flask route, defining the main behaviour of the webserver:
@app.route
(
"
/handle_message
"
,
methods
=
[
'
POST
'
])
@app.route
(
"
/handle_message
"
,
methods
=
[
'
POST
'
])
def
handleMessage
():
def
handleMessage
():
img_path
=
""
new_message
=
request
.
form
[
'
msg
'
]
new_message
=
request
.
form
[
'
msg
'
]
if
(
'
file
'
in
request
.
files
and
request
.
files
[
'
file
'
]):
image
=
request
.
files
[
'
file
'
]
img_path
=
os
.
path
.
join
(
UPLOAD_FOLDER
,
image
.
filename
)
image
.
save
(
img_path
)
if
new_message
:
if
new_message
:
append_message_to_file
(
new_message
)
append_message_to_file
(
img_path
,
new_message
)
return
render_template
(
'
handle_message.html
'
,
message
=
new_message
)
return
render_template
(
'
handle_message.html
'
,
message
=
new_message
)
...
...
This diff is collapsed.
Click to expand it.
templates/home.html
+
6
−
5
View file @
5695bf39
...
@@ -3,22 +3,23 @@
...
@@ -3,22 +3,23 @@
https://jinja.palletsprojects.com/en/3.0.x/ -->
https://jinja.palletsprojects.com/en/3.0.x/ -->
<title>
Message board
</title>
<title>
Message board
</title>
<body>
<body>
<h4>
Welcome to Message board
</h4>
<h4>
Welcome to
Pelle's
Message board
</h4>
There are {{ messages|length }} messages on the board.
</
br>
There are {{ messages|length }} messages on the board.
</
br>
<h4>
Here are the last 10:
</h4>
<h4>
Here are the last 10:
</h4>
<ul>
<ul>
{% for m in messages[-9:]|reverse %}
{% for m in messages[-9:]|reverse %}
<li>
<li>
"{{m.content}}"
<small>
Posted on {{m.timestamp}}
</small>
"{{m.content}}"
<small>
Posted on {{m.timestamp}}
</small>
<img
src=
"{{ m.img_path }}"
width=
"500"
>
</li>
</li>
{% endfor %}
{% endfor %}
</ul>
</ul>
<h4>
Enter a new message
</h4>
<h4>
Enter a new message
</h4>
<form
action=
"/handle_message"
method=
"post"
>
<form
action=
"/handle_message"
method=
"post"
enctype=
multipart/form-data
>
<label
>
Your message:
</label><br>
<label
>
Your message:
</label><br>
<input
type=
"text"
name=
"msg"
><br>
<input
type=
"text"
name=
"msg"
><br>
<input
type=
"file"
name=
"file"
><br>
<input
type=
"submit"
value=
"Submit"
>
<input
type=
"submit"
value=
"Submit"
>
</form>
</form>
</body>
</body>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment