Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Jackie Santana
Jackie Santana
7,403 Points

How do you link a python script to your html file?

How do you link a python script to your html file? just as how you link css, javascript or anything else..?

2 Answers

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

Python is normally not run in the browser the same way that HTML, CSS, and JavaScript are. (There are some tools like WebAssembly that can let you use other languages like Python in the browser. But it's probably not worth the trouble at this point unless you have a really good reason).

Python is useful for your server side code. The server is where you can do things like user authentication, connecting to your database. I recommend the Flask track for that. Data Science/Machine Learning is another very common use of Python.

Let me know if you have more questions.

Jackie Santana
Jackie Santana
7,403 Points

so what if i want to run functions ..functions that will display things on my webpage.. , reason why i ask is because i'm new to python. i know for example with javascript, i can create a (hide) function that can hide something on my html web page..,so my question is could i use a python function the same way to enable something on my html page? it's just odd that you cannot link it.

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

Python functions in your server side code will be used to different kinds of things. Let's say we have a To Do list app. You can have a database that stores everyone's todos. The server and database are running on a "cloud" computer (in some data center run by Amazon for example).

Your HTML page can have a form to create a new todo. When someone fills out the form and hits 'submit', you can have it send a message, an HTTP POST request, to your server with that data. The server has some code that is constantly running, listening for new messages. When it gets message with a new todo, it calls a function that adds the new todo to the database. Your HTML page can also send a message to the server saying "get me all my todos" and it would give them back the full list.

The benefit of doing something like this is that you could save the todos permanently somewhere that your user can access later from anywhere, even if they close their browser, even if they open the page on a different computer or on their phone. If you only build a website in HTML, CSS, JavaScript, if you only have "front end" code, which runs in your user's browser, you're limited what you can build. When you start bringing in servers and databases it opens up a lot more possibilities.

Jackie Santana
Jackie Santana
7,403 Points

gotchu, makes sense now, thanks bro