8000 Added Pipfile to async scraper in section12; PDF to section13; and Fl… · py-guy/complete-python-course@c56e18f · GitHub
[go: up one dir, main page]

Skip to content

Commit c56e18f

Browse files
committed
Added Pipfile to async scraper in section12; PDF to section13; and Flask project in section14.
1 parent cb0669c commit c56e18f

File tree

42 files changed

+900
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+900
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Subtitles
12
documents/
23
.env/
34
logs.txt
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[[source]]
2+
3+
url = "https://pypi.python.org/simple"
4+
verify_ssl = true
5+
name = "pypi"
6+
7+
8+
[packages]
9+
10+
aiohttp = "==3.0.5"
11+
requests = "==2.18.4"
12+
beautifulsoup4 = "==4.6.0"
13+
14+
15+
[dev-packages]
16+

section12/projects/async_scraping/Pipfile.lock

Lines changed: 154 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

section13/using_pipenv.pdf

859 KB
Binary file not shown.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from flask import Flask
2+
3+
# We create a new Flask app with a unique name (normally we use '__main__' since that's always unique across all files imported in the project).
4+
app = Flask(__name__)
5+
6+
7+
# This decorator registers the '/' route with our app.
8+
# When a user visits the '/' route (which is the homepage), the home() function will execute.
9+
@app.route('/')
10+
def home():
11+
"""
12+
This function will run when the user visits the '/' route (the homepage).
13+
It just returns 'Hello, world!' as any good first application should!
14+
"""
15+
return 'Hello, world!'
16+
17+
18+
# Only run the code inside the if statement if we've execute this file.
19+
# We don't want to run our app if we have imported this file, as running the app
20+
# would block and prevent importing this file, since it runs until the app is shut down.
21+
if __name__ == '__main__':
22+
# Run the app.
23+
# The debug=True flag is just here for development purposes.
24+
# It gives us more information if an error happens.
25+
app.run(debug=True)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from flask import Flask
2+
3+
4+
app = Flask(__name__)
5+
posts = {
6+
0: {
7+
'title': 'Hello, world',
8+
'content': 'This is my first blog post!'
9+
}
10+
}
11+
12+
13+
@app.route('/')
14+
def home():
15+
return 'Hello, world!'
16+
17+
18+
# This route expects to be in the format of /post/0 (for example).
19+
# Then it will pass 0 as argument to the post() function.
20+
@app.route('/post/<int:post_id>')
21+
def post(post_id):
22+
"""
23+
This function runs when a user visits route such as:
24+
25+
- /post/0
26+
- /post/2
27+
- /post/99
28+
29+
But not:
30+
31+
- /post/a
32+
- /post/something/else
33+
- /posts/1
34+
35+
Then we get the 0 as a number (not a string!) as argument, so we can use it.
36+
"""
37+
post = posts.get(post_id) # Retrieve the post from our global posts dictionary by the ID passed in as argument.
38+
return f"Post {post['title']}, content:\n\n{post['content']}" # Return the title and content formatted a bit nicer.
39+
40+
41+
if __name__ == '__main__':
42+
app.run(debug=True)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from flask import Flask, render_template
2+
3+
4+
app = Flask(__name__)
5+
posts = {
6+
0: {
7+
'title': 'Hello, world',
8+
'content': 'This is my first blog post!'
9+
}
10+
}
11+
12+
13+
@app.route('/')
14+
def home():
15+
return 'Hello, world!'
16+
17+
18+
@app.route('/post/<int:post_id>')
19+
def post(post_id):
20+
"""
21+
This function renders a template from the `templates` folder in the directory of app.py.
22+
It will find the `post.jinja2` template and render it with the data passed in.
23+
24+
Look at `post.jinja2` for information on how the variable `post` gets used there.
25+
"""
26+
return render_template('post.jinja2', post=posts.get(post_id))
27+
28+
29+
if __name__ == '__main__':
30+
app.run(debug=True)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<head></head>
3+
<body>
4+
5+
<!-- This is a HTML comment, so it doesn't get executed by the browser! -->
6+
<!-- The below are variables that will get replaced by their values (as if they were Python). They must be inside {{ }} in order to do that. -->
7+
<h1>{{ post['title'] }}</h1>
8+
<p>{{ post['content'] }}</p>
9+
10+
</body>
11+
</html>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from flask import Flask, render_template
2+
3+
4+
app = Flask(__name__)
5+
posts = {
6+
0: {
7+
'title': 'Hello, world',
8+
'content': 'This is my first blog post!'
9+
}
10+
}
11+
12+
13+
@app.route('/')
14+
def home():
15+
return 'Hello, world!'
16+
17+
18+
@app.route('/post/<int:post_id>')
19+
def post(post_id):
20+
post = posts.get(post_id)
21+
# If the post was not found, then we can render a different page instead—the user will see an error page.
22+
if not post:
23+
# Here we pass another variable, the string `message`.
24+
# It can be used inside the jinja template.
25+
return render_template('404.jinja2', message=f'A post with id {post_id} was not found.')
26+
return render_template('post.jinja2', post=post)
27+
28+
29+
if __name__ == '__main__':
30+
app.run(debug=True)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!-- Use the base.jinja2 template. We can then override blocks defined there and "fill them in". -->
2+
{% extends 'base.jinja2' %}
3+
4+
<!-- Replace the `content` block with whatever we put inside it... -->
5+
<!-- In this case a title saying "404", and the contents of the `message` variable. -->
6+
{% block content %}
7+
<h1>404 — Post not found</h1>
8+
<p>{{ message }}</p>
9+
{% endblock %}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<head></head>
3+
<body>
4+
<!-- This is a generic Jinja2 template which defines a block. It doesn't use any variables (so it won't actually show any content.) -->
5+
<!-- Other templates can however fill in this block with their own content. That means we won't need to duplicate e.g. `DOCTYPE`.-->
6+
{% block content %}
7+
8+
{% endblock %}
9+
</body>
10+
</html>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!-- Use the base.jinja2 template. We can then override blocks defined there and "fill them in". -->
2+
{% extends 'base.jinja2' %}
3+
4+
<!-- Replace the `content` block with whatever we put inside it... -->
5+
<!-- In this case, a title with the `post['title']` and a paragraph with the `post['content']`. -->
6+
{% block content %}
7+
<h1>{{ post['title'] }}</h1>
8+
<p>{{ post['content'] }}</p>
9+
{% endblock %}

0 commit comments

Comments
 (0)
0