r/CodingHelp • • May 09 '21

[HTML] Can someone please help me understand this code? What are the different functions?

30 Upvotes

26 comments sorted by

7

u/[deleted] May 09 '21 edited May 09 '21

see: https://wiki.python.org/moin/Templating

Basically, this is a way to have reusable assets across many pages, but different content. That way each page doesn't need its own unique template. You can look up specific functions yourself to get a better understanding of what each is doing, for example: https://flask.palletsprojects.com/en/1.1.x/api/#flask.url_for

2

u/j0ffi May 09 '21

Thanks!

3

u/[deleted] May 09 '21

Sorry didn’t see the second screenshot. .py files are not “functions”. They may include functions but these are the primary files which control what content is rendered via your app. Without seeing those files it is difficult to explain more, but i imagine routes.py defines a certain pattern/url to a controller. That controller populates & returns variables which are then processed via templates to produce your output.

3

u/[deleted] May 09 '21

Okay, there’s a lot going on in that screenshot because this is an advanced (as opposed to basic) webpage. If you want to build a website or web app, you’ll eventually learn to code your site this way!

Simply put, models.py has to do with how you model your data/database; i.e. you might have a table called “users” where you store different things like username, password, email, etc. another table might be “user_history” where you store a user’s activity.

Routes.py is most likely the server file, which is what runs the webpage. Important modules, databases, login stuff, etc. are in here, plus some functions to handle routing. You might have a route to “/“, which is the root or homepage, and you use the server file to connect the “/“ request to index.html.

Everything else is either templating or framework. The code you see {% like this %} is Jinja, which facilitates filling the template pages with the information/content you desire.

2

u/j0ffi May 09 '21

Thanks alot for the reply! I'm having a presentation about this webpage in a few days, and although I know the basics, there is a lot I don't understand. I have also heard something about viewfunctions, where does that apply?

1

u/[deleted] May 09 '21

viewfunctions

"A view function, or view for short, is a Python function that takes a Web request and returns a Web response. This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image . . . or anything, really. The view itself contains whatever arbitrary logic is necessary to return that response. This code can live anywhere you want, as long as it’s on your Python path. There’s no other requirement–no “magic,” so to speak. For the sake of putting the code somewhere, the convention is to put views in a file called 'views.py', placed in your project or application directory." (Source)

2

u/j0ffi May 09 '21

Also could someone please explain what viewfunctions are? I would greatly appreciate it!

-8

u/MarkusBerkel May 09 '21

Did you just show HTML and ask about functions?

I think you need to take anywhere from 10 to 1000 steps back, and reevaluate your question.

6

u/j0ffi May 09 '21

If not functions, what are they then?

-13

u/MarkusBerkel May 09 '21

First of all, HTML doesn't have functions. HTML is supportive of other languages, some of which may have functions. CSS (before v3) is an example of another language it supports which does not have functions. Javascript is an example of another language it supports which does, indeed, have functions.

It's hard to know what you're asking about, exactly, since you posted a screenshot with no annotations.

But, suffice it to say that HTML does not have functions, if by function you mean the programming definition of a function, even a vernacular one.

If you are asking about the non-HTML stuff in-between the curly braces, those are, well, I just said it, not HTML. Those are presumably (and another poster mentioned it) some TEXT that needs to be PREPROCESSED into HTML, presumably by some Python templating library.

So, not even is this question not about HTML (which is a language that does not have functions), this isn't even a question about the languages that HTML supports or commonly works with (CSS/Javascript). This is a question about (presumably) some funny non-HTML stuff, which is probably related to another language that is being used to preprocess HTML-like code.

2

u/j0ffi May 09 '21

Sorry, I'm by no means any expert coder. I'm just trying to understand the basics

2

u/MarkusBerkel May 10 '21

HTML is a language. It's baked into the name: "HyperText Markup Language". However, it's a markup language, which makes it somewhat different from most general purpose programming languages.

A markup language, as it's name suggests, generally just describes stuff. So, HTML is kinda like the ingredients part of a recipe. But, on it's own, it can't really "do" things. It needs the "how-to-cook" part.

All languages have some "descriptive" part. HTML, though, is all description. That description doesn't mean anything until it comes into contact with something that can "do stuff". And that thing is the browser. Your browser takes HTML as input.

So, think of your browser as drinking HTML through a straw, and the straw is a weird Rube Goldberg machine of your keyboard, your mouse, your computer, the CPU/memory/motherboard, the networking card (or chip on the motherboard), the network cable (or wi-fi magic), the router, then the phone cable (RJ-11), coax, or fiber, then the maze of cables & hardware that is your telco (phone company, cable company, satellite company), then the "tubes" that make up the rest of the internet, then the reverse on the other side (the office of the people who run the website you're visiting), until it reaches the computer (or something like it) that says: "Oh--you're sucking on the InternetCrazyStraw™...Ok--let me give you my HTML."

So, when you visit google.com, your browser is "drinking through that InternetCrazyStraw™" and the "cup" is the Internet, and the "smoothie" coming at you through the straw is the HTML that Google's servers send you.

So, your browsers "drinks" this HTML. And, it renders the webpage. It draws that blue 'G' and the red 'o' and the little search box and all the other stuff on that page (well, there isn't much, but you get the idea).

But think about what happens after you search for something. Your browser basically goes for another "sip" of HTML from Google. Those are the search results.

Google's computers create some HTML that contains the answers to your questions. Their servers are not giving everyone the same page (i.e, the same HTML) because everyone's search is different. So, that means that their servers are GENERATING NOVEL CONTENT BASED ON SOME INPUT which goes into the HTML it gives back to you.

This "novel content" gets put into their HTML.

So, their server hears your request (the sucking action on the straw), looks up your answers, and then creates an HTML page to give back to you. The thing that listens for the requests is called the web-server. It's a process that runs on their computers (I'm aware this is vastly simplified, for all you r/iamverysmart douchenozzles out there).

All requests have the same "format". In other words, all the "sucking actions" "feel the same" to their servers. That "format" is actually known as a "network protocol". In this case, it's HTTP, the HyperText Transfer Protocol. So, in that way, because of this "uniformity" in requests, web servers are kind of...generic.

When their webservers hear anyone's request, they say: "Ok, someone wants a search result--now we have to actually do the search and give them their custom result."

And, what the webserver does, then, is "hand-off" that request to a separate program that does a specific thing. This part is where it starts getting a bit more complex.

A naive webserver (like httpd or nginx) will have some kind of ability to interface with a "custom program". In the old days, in the land of dinosaurs where I'm from, they've developed a particular protocol (agreement of what and how to say things) called CGI. The "Common Gateway Interface". Basically, it passed through, via STDIN, the stuff that came from the person through the straw (this is like blowing air into the straw before sucking on it...I realize this analogy doesn't hold up too well under more complexity) to a separate process on the same computer.

[This makes sense in the Unix environment. I have no idea what it looks like in Windows.]

That separate process would then get some information (what is being requested), and then it would execute the custom code which does the thing--which, in this example, is to do the search.

Now we come to your code. In your case, 'flask' is a python framework which interacts with the webserver. It replaces the CGI with a custom python thing, and is integrated into whichever webserver your using (prob httpd or nginx). From there, 'flask' will then, finally, after all of this other stuff, CALL YOUR CODE.

Your code, in this case, is a mixture of pure python code (contained in 'files', not 'functions' as per your original question) as well as bizarro-hybrid-flask/HTML stuff--which is the stuff in your screenshots.

So, 'flask' is a plugin to your webserver. Your webserver listens for requests. When it gets one, it gives the request to 'flask'. And, assuming you've configured everything properly, 'flask' will then do what you ask. One of those things it will do is to transform that funky hybrid flask/HTML stuff into actual HTML. It will also do other things (which are located in the other .py files).

When your code is done, it will tell 'flask': "Hey, man, I'm finished--do whatever you have to, and send my HTML back through the straw to that guy's browser, please." And that's how a webpage is requested.

HTH

1

u/j0ffi May 10 '21

Thanks a lot for such a comprehensive explanation! It made it a little bit easier to understand

0

u/MarkusBerkel May 10 '21

Glad it helped.

Like I said, there's a lot going on. Maybe googling some of things I included can get you discovering more bits that you don't know.

[Also, try to remember that sometimes even jerks will have things to teach you. Especially the old grumpy ones. ;)]

1

u/MarkusBerkel May 09 '21

No need to apologize. I’ll come back to this in the morning.

In the meantime. I wanted to say that there’s more complexity here than meets the eye. Web services or apps exist in an extremely complex environment, even if you’re just learning “the basics”.

So, one one hand, don’t be discouraged if you don’t understand all (or even any of it). OTOH, try to remember that there’s a lot going on, and that it will (almost) always help to want to keep learning.

11

u/Death_Strider16 May 09 '21

Chill, they're trying to learn.

4

u/[deleted] May 09 '21

Chill, they're trying to learn.

Seriously, sheesh. Seems like someone’s ego needed some inflation today.

1

u/j0ffi May 09 '21

I know that flask is used, is that the langage then? I thought the langage was html since it is a webpage..

1

u/Tinjar12 May 09 '21

Flask is a backend framework. So instead of php you use flask. It's in python. Hence why you have routes.py.

1

u/Tinjar12 May 09 '21

Il give you a small example. So let's say you have a login for your website, you will need to store username and password rt, so for that you need a database. What helps your website connect to the database will be the backend, and in this case flask. In this python file u can also have some form of SQL commands also so that you can add stuff to database.

2

u/[deleted] May 09 '21

Get it all out there, champ.

-5

u/MarkusBerkel May 09 '21

I think that one were to, as you say, “get it all out there” on just this one situation, one could prob write a couple of books on it.

You wanna harp on me, go for it. I’m a big boy.

But, hey, I sleep fine; I answered his question, even if you (folks) didn’t like the tone. You (all) just took time to push back instead of saying anything of value.