How to store a portion of a result in python? - python

I have this code to get download links via libgen api.
#app.route("/api/default")
def titleSearch():
title = request.args.get('query')
s = LibgenSearch()
results = s.search_title(title)
item_to_download = results[0]
download_links = s.resolve_download_links(item_to_download)
return render_template(
"results.html", results=results, download_links=download_links, title=title,
)
results.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>SUSBOOKS</title>
<link rel="stylesheet" href="{{ url_for('static', filename='bootstrap.min.css') }}" />
<link rel="stylesheet" href="{{ url_for('static', filename='mdb.dark.min.css') }}" />
<link rel="stylesheet" href="{{ url_for('static', filename='all.min.css') }}" />
<style>
body {
background-color:#000000 ;
}
</style>
</head>
<body>
<p>{{download_links}}</p>
</body>
download_links comes out as
{'GET': 'http://62.182.86.140/main/265000/ef7a3a2ecf51fa41f621f4e8b17df828/%28Saddleback%27s%20Focus%20on%20Reading%20Study%20Guides%29%20Lisa%20Mccarty%20-%20To%20Kill%20a%20Mockingbird-Saddleback%20Educational%20Publishing%2C%20Inc.%20%282006%29.pdf', 'Cloudflare': 'https://cloudflare-ipfs.com/ipfs/bafykbzacebtnktd2zpvrcygswfcsr6wfft6jjyes6qi7f4mmottq3lebrgpsc?filename=%28Saddleback%27s%20Focus%20on%20Reading%20Study%20Guides%29%20Lisa%20Mccarty%20-%20To%20Kill%20a%20Mockingbird-Saddleback%20Educational%20Publishing%2C%20Inc.%20%282006%29.pdf', 'IPFS.io': 'https://ipfs.io/ipfs/bafykbzacebtnktd2zpvrcygswfcsr6wfft6jjyes6qi7f4mmottq3lebrgpsc?filename=%28Saddleback%27s%20Focus%20on%20Reading%20Study%20Guides%29%20Lisa%20Mccarty%20-%20To%20Kill%20a%20Mockingbird-Saddleback%20Educational%20Publishing%2C%20Inc.%20%282006%29.pdf'}{'GET': 'http://62.182.86.140/main/265000/ef7a3a2ecf51fa41f621f4e8b17df828/%28Saddleback%27s%20Focus%20on%20Reading%20Study%20Guides%29%20Lisa%20Mccarty%20-%20To%20Kill%20a%20Mockingbird-Saddleback%20Educational%20Publishing%2C%20Inc.%20%282006%29.pdf', 'Cloudflare': 'https://cloudflare-ipfs.com/ipfs/bafykbzacebtnktd2zpvrcygswfcsr6wfft6jjyes6qi7f4mmottq3lebrgpsc?filename=%28Saddleback%27s%20Focus%20on%20Reading%20Study%20Guides%29%20Lisa%20Mccarty%20-%20To%20Kill%20a%20Mockingbird-Saddleback%20Educational%20Publishing%2C%20Inc.%20%282006%29.pdf', 'IPFS.io': 'https://ipfs.io/ipfs/bafykbzacebtnktd2zpvrcygswfcsr6wfft6jjyes6qi7f4mmottq3lebrgpsc?filename=%28Saddleback%27s%20Focus%20on%20Reading%20Study%20Guides%29%20Lisa%20Mccarty%20-%20To%20Kill%20a%20Mockingbird-Saddleback%20Educational%20Publishing%2C%20Inc.%20%282006%29.pdf'}
How would I go about making it output just the links hyperlinked so users can just click to download them?

In response, you have a dictionary which is key-value pairs.
reading key value of a dictionary:
my_dict = {
"link_1": "example_1.com",
"link_2": "example_2.com"
}
for key, value in my_dict.items():
print(key, value)
output
link_1, example_1.com
link_2, example_2.com
now you flask template you can do
{% for key, value in my_dict.items() %}
<p>{{ key }}, {{ value }}</p>
{% endfor %}
instead of
<p>{{ my_dict }}</p>

Related

Pandas Dataframe to searchable bootstrap table

I am wanting to take a dataframe and put it to a bootstrap table that is searchable. Right now I have this:
tester.html
<!DOCTYPE html>
<html lang="en">
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<head>
<title>W3.CSS Template</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="{{ url_for('static', filename='css/df_style.css') }}">
<style>
body {font-family: "Lato", sans-serif}
.mySlides {display: none}
</style>
</head>
<div align="center">
<br>
<br>
<br>
<br>
<br>
<br>
<table id="myTable">
<h1>
<!--Displaying the converted table-->
{% for table in tables %}
<h2>{{titles[loop.index]}}</h2>
{{ table|safe }}
{% endfor %}
</h1>
</table>
</div>
</body>
app.py
from app import app
from flask import Flask, render_template
#app.route("/")
def eod_coin():
return render_template('tester.html', tables=[eod.to_html()], titles=[''])
I have looked at the following How to make bootstrap-table-filter-control work with Flask, Jinja and Dataframe but can't seem to get the search function to work when I want to pass in a df and not building the data in my app.py

Template syntax error Could not parse the remainder: '("images/logo.png")' from 'static("images/logo.png")'. in django-jinja template?

<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="{{ static 'images/logo.png' }}" alt="Company Logo">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css"
rel="stylesheet" integrity="sha384-
1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<h1>Hello, world!</h1>
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous"></script>
<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/#popperjs/core#2.10.2/dist/umd/popper.min.js"
integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.min.js"
integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13"
crossorigin="anonymous"></script>
-->
</body>
</html>
when including the static files in the Django-jinja template it shows an error. If I removed that line It worked. But I want to load my static files in jinja template.
First you have to load static at top of your html page
{% load static %}
Then you have to use {% %} instead of {{ }}
<link rel="icon" href="{% static 'images/logo.png' %}" alt="Company Logo">
you need to add {% load static %} first

Django csrf_token error, while I wrote it

Django csrf_token error, while I wrote it in my HTML file, but why does this error message poped up when I wrote that csrf_token?
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static 'f/ask/ask.css' %}">
<title>BareTalk</title>
</head>
<body>
{% block content %}
<div id="wrapper">
<form method="POST" action="">{% csrf_token %}
{{ form }}
<!-- TODO: input submit Reg() function javascript -->
<button name="final" id="final">Ask</button>
</div>
</form>
</div>
{% endblock %}
</body>
<script src="{% static 'f/ask/ask.js' %}"></script>
</html>
paste this code and try again ->
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static 'f/ask/ask.css' %}">
<title>BareTalk</title>
</head>
<body>
<div id="wrapper">
<form method="POST">
{% csrf_token %}
{{ form }}
<!-- TODO: input submit Reg() function javascript -->
<button name="final" id="final">Ask</button>
</form>
</div>
</body>
<script src="{% static 'f/ask/ask.js' %}"></script>
</html>

Django variables won't render in templates

Django version 2.1.3
Python version 3.7
Writing out sample code just to get an understanding of Django. Right now I'm on Templates and I'm having 0 luck when it come to rendering variables.
In views.py folder I've created a little dictionary and passed it through under the variable content
from django.shortcuts import render
# Create your views here.
posts = [
{
'Title': 'Challanger 1',
'Name': 'Sammy',
'Age': '33',
'Food': 'Seafood'
},
{
'Title': 'Challanger 2',
'Name': 'Sammy',
'Age': '33',
'Food': 'Seafood'
}
]
def home(request):
content = {
'posts': posts
}
return render(request, 'blog/home.html', content)
In my home.htmlfile, I've added a few 123 next to my {{variable}}to make sure the .html file is connecting to the view.py. When I py manage.py runserver, only the 123 is displayed but none of my {{variables}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
{% for post in posts %}
<p>123 {{ post.name }}</p>
<h1>123 {{ post.title }}</h1>
<h1>123 {{ post.age }}</h1>
{% endfor %}
</body>
</html>
localhost:8000 produces:
123
123
123
123
123
123
When I open view-source from the browser:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p>123 </p>
<h1>123 </h1>
<h1>123 </h1>
<p>123 </p>
<h1>123 </h1>
<h1>123 </h1>
</body>
</html>
Side note: When I'm in the home.html file I've noticed that only {{post.title}} and {{post.name}} auto-fill and {{post.age}} and {{post.food}} never auto-fill.
There are also times when I've deleted everything in the home.html file and none of the {{variables}} auto-fill when I rewrite out the code. Either way the ending result is still the same, variables wont load.
You're using post.name (all lowercase) in the template, but you defined the dictionaries in the python code to have Name (uppercase N) as the key.
Try using {{ post.Name }}.
Here is the problem, Dictionary Keys Are Case-Sensitive.
try below code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
{% for post in posts %}
<p>123 {{ post.Name }}</p>
<h1>123 {{ post.Title }}</h1>
<h1>123 {{ post.Age }}</h1>
{% endfor %}
</body>
</html>

Flask html templates inheritance issue - mixed elements

everyone! I have an issue when inheriting from another template in Flask. My first file layout.html looks like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flask</title>
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<header>
<h1>Some header</h1>
</header>
<content>
{% block content %}{% endblock %}
</content>
</body>
</html>
Second one "main.html":
{% extends "layout.html" %}
{% block content %}<p>test</p>{% endblock %}
Everything looks ok but when I load the page in browser the elements looks like this(everything from head is moved to body:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<meta charset="UTF-8">
<title>Flask</title>
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
<script type="text/javascript" src="script.js"></script>
<header>
<h1>Some header</h1>
</header>
<content>
<p>test</p>
</content>
</body>
</html>
Can anyone explain why this happens?
Maybe a little bit too late... The issue was, that I'd changed my IDE. Before I'd used PyCharm, then I switched to Visual Studio. It looks like they both use different encoding and something broke during migration. Creating new file and copying content was the solution.

Categories

Resources