0

I have a base.py file with lots of lists (over 50)

model1 = [
    'https://',
    'https://',
    'https://',
    'https://',
    'https://',
    'https://',
    'https://'
]

model2 = [
    'https://',
    'https://',
    'https://',
    'https://',
    'https://',
    'https://',
    'https://'
]

Also, another func.py file contains functions for handling url from the base.py file

I need to derive links from the base.py file and the results of functions from the func.py file in the HTML template

Using Flask, I output the links as follows

import base

def index():
    return render_template("index.html",
        url = base)

Question: How to transfer the desired url of a specific model from a specific list from the base.py file to the func.py file function and display the result of this function in the HTML template using Flask?

HTML template

<tr>
<td>Model</td>
<td><a href="{{ url.model1[0] }}" target="_blank">{{ price0 of func.py }}</a></td>
<td><a href="{{ url.model1[1] }}" target="_blank">{{ price1 of func.py }}</a></td>
<td><a href="{{ url.model1[2] }}" target="_blank">{{ price2 of func.py }}</a></td>
<td><a href="{{ url.model1[3] }}" target="_blank">{{ price3 of func.py }}</a></td>
<td><a href="{{ url.model1[4] }}" target="_blank">{{ price4 of func.py }}</a></td>
<td><a href="{{ url.model1[5] }}" target="_blank">{{ price5 of func.py }}</a></td>
<td><a href="{{ url.model1[6] }}" target="_blank">{{ price6 of func.py }}</a></td>
</tr>

The func.py file contains 6 functions, for each function in the list from the base.py file

func.py

def url1 (murl):
#####################
    print(price)

def url2 (murl):
#####################
    print(price)
W1ns
  • 13
  • 3
  • you should be doing most of that logic in your `index` function and then return a dictionary or a list of dictionarys with what you need - something in the format of `data = {'url': url.model[0], 'result': result_of_func.py}` then you can access these in your templates. you then pass it as you are `return render_template("index.html", data=data)` – Craicerjack Dec 04 '19 at 12:04
  • 1
    It would be useful to know what does `func.py` look like and what it does. Is it a module with a single function? Are there various functions and every one of these models need to be parsed with a different one? – Maciej B. Nowak Dec 04 '19 at 12:08
  • There needs to be a better definition of your problem as its not really clear what part of your issue your looking for a solution for. You can create custom filters in Jinja that will apply a function in your template to your value - https://jinja.palletsprojects.com/en/2.10.x/api/#custom-filters – Craicerjack Dec 04 '19 at 12:11
  • @DavidGildour Do the functions in `func.py` just return a price? Is there a difference in them? Are the models in `base.py` just a list of links? How do you get the price from a link? – Craicerjack Dec 04 '19 at 12:57
  • @Craicerjack 1 model - 6 links - 6 different sites Models more than 50 Each link has its own function that parses the price from the site using BeautifulSoup. – W1ns Dec 04 '19 at 13:06
  • okay - so you need to do the beautiful soup functions in your index function and then send it to your template – Craicerjack Dec 04 '19 at 13:08

1 Answers1

0

So you need to do the logic in your view function.
Create a new list of models with each model having its url and price associated together - in a dictionary
Then once youve created your new data you pass it to your template where you can loop through it and access the values

If each url has its own function you can store your functions in a list and access the relevant ones by the index of the url you are looping through
See this question - Calling functions by array index in Python

func.py

my_fn = [func1, func2, func3, etc]  
import base
import func

def index():
    models_with_links = []
    for model in base:
        # creating a new dict with the associate link and price with list comprehension
        new_model = [{"url": link, "price": my_fn[idx](link)} for idx, link in enumerate(model)]
        models_with_links.append(new_model)
    return render_template("index.html", models=models_with_links)

You can then use loops in Jinja So you can loop through your urls which contains multiple models and then loop through the urls of those models:

<tr>
  <td>Model</td>
  {% for model_list in models %}
    {% for model in model_list %}
      <td>
        <a href="{{ model.url }}" target="_blank">
          {{ model.price }}
        </a>
      </td>
    {% endfor %}
  {% endfor %}
</tr>
Craicerjack
  • 6,203
  • 2
  • 31
  • 39
  • base is a module so pylint writes "object not repeatable"? `dir(base)` returns the lists out of order and adds the functions `___all___, __doc__`, etc. How can I list the lists from the base file? – W1ns Dec 05 '19 at 08:09
  • put them all in a dictionary, the model name being the key and the list being the value and then you can import that dictionary from the module – Craicerjack Dec 05 '19 at 10:24