Python Forum
Displaying search results - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Displaying search results (/thread-27661.html)

Pages: 1 2 3 4 5 6 7 8 9


RE: Displaying search results - card51shor - Jun-16-2020

(Jun-16-2020, 06:29 AM)ndc85430 Wrote: For a start, your template doesn't have any variables in it, does it? Did you even try the example from the Flask docs like I suggested? It shows you all the pieces - the template and passing variable data to it. OK, the example is a bit different, since the data are passed in the request URL, but that's really not the point. It's just some data that's passed to render_template and the final HTML is generated from that.

How have you been learning how to use Flask, or build a web application in general? It seems like there are a lot of gaps. Maybe try going through Grinberg's tutorial, because I think he explains quite a lot.


I've just been teaching myself by trying to figure out this project. I've got about half of it done so far I try almost every night but I always hit a wall. I just don't get it. If I look at an example that isn't doing what I'm trying to do - it just ends up confusing me even more.

Why would I need variables in the HTML? I already have the data that I need in the variable "b" in python. I just want to print out the values for the users to see from the search.


RE: Displaying search results - buran - Jun-16-2020

(Jun-16-2020, 06:36 AM)card51shor Wrote: Why would I need variables in the HTML? I already have the data that I need in the variable "b" in python. I just want to print out the values for the users to see from the search.
How would user "see" the result of the search - on the page (displayed using HTML template). They don't see what is printed in the terminal console on the server.
How would you display search result in the page - by passing variables when you render the tempalte. As the name indicates - it's a template - it has fixed part that looks the same for every search result and you have dynamic part - the search result itself. There should be placeholders/variables in the HTML template and you need to fill-in these placeholders by passing values when render the template


RE: Displaying search results - card51shor - Jun-16-2020

OK that makes sense - thank you. However, how do I print the actual data for the user to see on the html?

I have to run SQL to get the values right? I'm getting the values in python already. I know the user can't see them in the terminal - I was wondering how I can make it so they can see the values.

Creating HTML elements seems like a time consuming and clunky way of doing it. I"m not sure if that's what you're suggesting.


RE: Displaying search results - ndc85430 - Jun-16-2020

Like I told you, figure out how to do templating. I figure that the problem of writing this web app is quite big for you and you're not able to break it down into small pieces. When you started programming, did you write a program that asked a user's name and then greeted them (e.g. if you entered "Alice", it would say "Hello, Alice", or if you entered "Bob", it would say "Hello, Bob")? If you haven't written web apps before, I'd basically start with doing the web app version of that - getting that string displayed in a browser. The templating example in the Flask docs does exactly that. Trying to do too many things at once just leads to confusion I think.

(Jun-16-2020, 06:36 AM)card51shor Wrote: I've just been teaching myself by trying to figure out this project.

What I was getting at really is that your way of learning seems to be very unstructured and you're missing key knowledge because of that. The fact that you don't understand why templating is necessary (see below) is a sign.

Quote:Why would I need variables in the HTML? I already have the data that I need in the variable "b" in python. I just want to print out the values for the users to see from the search.

Think about it: your search query is already reasonably complex. One user might search for "Refactoring", another might search for "Kent Beck" and the possibilities are endless. You just can't have static pages to take care of all those possibilities, so the pages that the browser gets need to be dynamically created. OK, so the first way to do that is just by using string concatenation (or interpolation) to make the structure of the page and insert the data in the right places. Of course, you'd be using variables there. Nobody really does it this way any more, not least because it doesn't scale. We use templates these days, which again make use of variables to put the data in the right places. It's a better approach also because you separate how you get the data (and whatever calculations, transformations, etc. you need to do on it) from how it's presented to the user. Separation of concerns is really important in software - it helps to allow you to change one thing without affecting the other.


RE: Displaying search results - card51shor - Jun-16-2020

I looked at the template and I understand it. It's simple I get it. I just don't get how to put my data on the actual website.

I can get it to print out to terminal - how do I print it to the actual html so the user can see it?

I can't use templates since it is a long list of values they are getting not just one row of data.

Or, if I can use templates - I don't know how to use it in this manner. I can use it in a simple manner like displaying one word. But how do I display a list that has links built into it?

Where's the syntax for how I would do that?


RE: Displaying search results - ndc85430 - Jun-16-2020

The Jinja2 website (https://jinja.palletsprojects.com/en/2.11.x/) is linked to from the Flask docs and there's helpfully a section there titled "Template Designer Documentation" that has lots of useful info. Even Miguel Grinberg's blog post on templates goes into more detail than the Flask docs.


RE: Displaying search results - card51shor - Jun-16-2020

I mean thanks a lot for trying to help - but that's like 50 pages of information. I wouldn't even know where to start. Just looking at the contents is daunting to me.

Is there any easier way you guys can think of? I have about 14 days to finish this and I can't start learning whole new languages like Jinga and doing long tutorials. This is already basically a tutorial that I'm doing to learn python!

The thing is - I already have the data in a variable in python. I'm already able to print out to a terminal. Now, I know languages like php and javascript where u can just use a different function to print out to either the terminal or to the actual page.

Python doesn't have anything like that?

Why do I have to go around passing variables through the html when I've already done the Python code to capture the data?

Seems now I just need to print the data to the user, right?


RE: Displaying search results - SheeppOSU - Jun-16-2020

(Jun-16-2020, 07:34 AM)card51shor Wrote: I mean thanks a lot for trying to help - but that's like 50 pages of information. I wouldn't even know where to start. Just looking at the contents is daunting to me.
You don't have to read it all. He said go to this section right here. You don't have to read through all of that, just read the first couple paragraphs. Surely you can read just that much. In fact, all the info you need is in just the first code block. You're kinda coming across as if you're not even trying to read through some of it. Documentation can be really useful. Either you find a video or read the docs, and the person in that video most likely read through the docs to get all the information. More often then not the docs will cover way more than what's covered in videos.


RE: Displaying search results - ndc85430 - Jun-16-2020

(Jun-16-2020, 07:34 AM)card51shor Wrote: Is there any easier way you guys can think of? I have about 14 days to finish this and I can't start learning whole new languages like Jinga and doing long tutorials. This is already basically a tutorial that I'm doing to learn python!

If you're doing some kind of course, haven't you learnt the stuff you need? If you're really struggling, perhaps you should speak to your course leaders about it.

The modern way of dynamically creating web pages is to use templates and I've explained the reasons for that above. I'm not familiar with PHP or JS stacks, but I'd be very surprised if they didn't do things this way. I've built web apps for quite a while in different technologies (Python and on the JVM (Java, Scala, Kotlin, Clojure)) and the basic idea is the same.


RE: Displaying search results - buran - Jun-16-2020

One of the things being a programmer is to work with documentation. Like every day, like A LOT. You need to learn how to read documentation, how to find relevant info in it, how to adapt examples in docs to your problem. That is essential skill. Even if you are familiar with given package, framework, language, etc. worked with it long time, still there are new features, changes in API, new better alternatives and so on.
Of course one can always search for help, suggestions, explanation of something that they don't grasp immediately or on their own but that again involves looking for additional resources and working with these resources.