Skip to content

Instantly share code, notes, and snippets.

@angelabauer
Created October 21, 2020 10:06
Show Gist options
  • Save angelabauer/df4ab0709c788cfdcf81a00316f4f018 to your computer and use it in GitHub Desktop.
Save angelabauer/df4ab0709c788cfdcf81a00316f4f018 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="https://fonts.googleapis.com/css2?family=Raleway" rel="stylesheet">
<link rel="stylesheet" href="static/css/styles.css">
</head>
<body>
<div class="wrapper">
<div class="top">
<div class="title"><h1>My Blog</h1></div>
</div>
{% for post in all_posts: %}
<div class="content">
<div class="card ">
<h2>{‌{ post.title }}</h2>
<p>{‌{ post.subtitle }}</p>
<a href="{‌{ url_for('show_post', index=post.id) }}">Read</a>
</div>
</div>
{% endfor %}
</div>
</body>
<footer>
<p>Made with ♥️ in London.</p>
</footer>
</html>
from flask import Flask, render_template
from post import Post
import requests
posts = requests.get("https://api.npoint.io/5abcca6f4e39b4955965").json()
post_objects = []
for post in posts:
post_obj = Post(post["id"], post["title"], post["subtitle"], post["body"])
post_objects.append(post_obj)
app = Flask(__name__)
@app.route('/')
def get_all_posts():
return render_template("index.html", all_posts=post_objects)
@app.route("/post/<int:index>")
def show_post(index):
requested_post = None
for blog_post in post_objects:
if blog_post.id == index:
requested_post = blog_post
return render_template("post.html", post=requested_post)
if __name__ == "__main__":
app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="https://fonts.googleapis.com/css2?family=Raleway" rel="stylesheet">
<link rel="stylesheet" href="static/css/styles.css">
</head>
<body>
<div class="wrapper">
<div class="top">
<div class="title"><h1>My Blog</h1></div>
</div>
<div class="content">
<div class="card">
<h1> {‌{ post.title }}</h1>
<h2> {‌{ post.subtitle }}</h2>
<p> {‌{ post.body }}</p>
</div>
</div>
</div>
</body>
<footer>
<p>Made with ♥️ in London.</p>
</footer>
</html>
class Post:
def __init__(self, post_id, title, subtitle, body):
self.id = post_id
self.title = title
self.subtitle = subtitle
self.body = body
@Hogbunike
Copy link

can someone please provide the static/styles.css folder....

@Hashirrazzaq256
Copy link

is anyone having issues with show_post function? the requested_post element is not sending data to post.html hence my posts are not displaying .CSS is displaying tho

@Oluwasegun122
Copy link

The post can't work because the API has just only one key which is the job_posting and the post is used to catch the keys in the API while those keys are not available in the API.

is anyone having issues with show_post function? the requested_post element is not sending data to post.html hence my posts are not displaying .CSS is displaying tho

The post can't work because the API has just only one key which is the job_posting and the post is used to catch the keys in the API while those keys are not available in the API.

@KomailMK
Copy link

KomailMK commented Aug 3, 2024

Check out my Code:
main.py

from flask import Flask, render_template
from post import Post
import datetime

year = datetime.datetime.now().year
app = Flask(__name__)
post = Post()


@app.route('/')
def home():
    return render_template("index.html", data=post.get_posts(), Y=year)
``
@app.route('/blog/<int:num>')
def post_blog(num):
    return render_template("post.html", data=post.get_posts(), N=num, Y=year)
    
if __name__ == "__main__":
    app.run(debug=True) 

post.py

import requests as req 


class Post:
    def __init__(self) -> None:
        self.data = ""
    def get_posts(self):
        self.data = req.get("https://api.npoint.io/c790b4d5cab58020d391").json()
        return self.data

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="https://fonts.googleapis.com/css2?family=Raleway" rel="stylesheet">
    <link rel="stylesheet" href="../static/css/styles.css">
</head>
<body>
<div class="wrapper">
    <div class="top">
        <div class="title"><h1>My Blog</h1></div>
    </div>
    {% for post in data: %}
        <div class="content">
            <div class="card">
                <h2> {{ post.title }}</h2>
                <p class="text">{{ post.subtitle }} </p>
                <a href="{{ url_for('post_blog', num=post.id) }}">Read</a>    
            </div>
        </div>
    {% endfor %}
    
</div>
</body>
<footer>
    <p>Made with ♥️ by MK.</p>
    <p> {{ Y }} All Rights Reserved </p>
</footer>
</html>

post.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="https://fonts.googleapis.com/css2?family=Raleway" rel="stylesheet">
    <link rel="stylesheet" href="../static/css/styles.css">
</head>
<body>
<div class="wrapper">
    <div class="top">
        <div class="title"><h1>My Blog</h1></div>
           </div>
        <div class="content">
            <div class="card">
                {% for post in data %}
                    {% if post.id == N %}
                        <h2> {{ post.title }}</h2>
                        <p class="text">{{ post.body }} </p>
                    {% endif %}
                {% endfor %}
            </div>

    </div>
</div>
</body>
<footer>
    <p>Made with ♥️ by MK.</p>
    <p> {{ Y }} All Rights Reserved </p>
</footer>
</html>

@Alex-V69
Copy link

main_d57.py
from flask import Flask, render_template, abort
import requests
from time import sleep


def get_info(url, retries=3, **kwargs):
    with requests.Session() as session:
        for i in range(retries):
            response = session.get(url, **kwargs)
            print("Status:", response.status_code)
            if response.status_code == 200:
                return response.json()
            elif i < retries - 1:
                sleep(1 * (i + 1))


app = Flask(__name__)
data = get_info('https://api.npoint.io/<your_unique_id_here>')


@app.route('/')
@app.route('/blog')
def home():
    if not data:
        abort(500)
    context = {
        'title': "My blog",
        'posts': data
    }
    return render_template("d57_index.html", **context)


@app.route('/post/<int:id>')
def blog_post(id):
    if id > len(data) or id == 0:
        abort(404)
    context = {
        'title': "My blog",
        'post': data[id-1]
    }
    return render_template("d57_post.html", **context)


if __name__ == "__main__":
    app.run(debug=True)
d57_index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block title %}{{ title }}{% endblock title %}</title>
    <link href="https://fonts.googleapis.com/css2?family=Raleway" rel="stylesheet">
    <link rel="stylesheet" href="../static/css_d57/styles.css">
</head>
<body>
<div class="wrapper">
    <div class="top">
        <div class="title"><h1>{% block heading %}{{ title }}{% endblock heading %}</h1></div>
    </div>
    {% block content %}
        {% for item in posts %}
            <div class="content">
                <div class="card">
                    <h2>{{ item.title }}</h2>
                    <p class="text">{{ item.subtitle }}</p>
                    <a href="{{ url_for('blog_post', id=item.id) }}" id="{{ item.id }}">Read</a>
                </div>
            </div>
        {% endfor %}
    {% endblock content %}
</div>
</body>
<footer>
    <p>Made by 💀 in 🏰</p>
</footer>
</html>
d57_post.html
{% extends "d57_index.html" %}
{% block title %}{{ title }} - {{ post.title }}{% endblock title %}
{% block heading %}
    <a href="{{ url_for('home') }}#{{ post.id }}" id="home">{{ title }}</a> - {{ post.title }}
{% endblock heading %}
{% block content %}
    <div class="content">
        <div class="card">
            <h2>{{ post.title }}</h2>
            <p>{{ post.body }}</p>
        </div>
    </div>
{% endblock content %}

@Oluwasegun122
Copy link

Oluwasegun122 commented Sep 1, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment