Skip to main content

Command Palette

Search for a command to run...

2 Ways To Start Using HTML With Python At Runtime

Published
6 min readView as Markdown
2 Ways To Start Using HTML With Python At Runtime

So, as a developer you might have wondered whether can we use python with HTML at runtime like on the running script and yes it is possible to use it and we will see how to use HTML with Python at runtime, I also learned about this at this moment of writing this post.

There are two ways I would say that you can use HTML with python, and they are :

  • Directly using python in HTML with the help of some modules.
  • Using Jinja with python (I prefer this)

We will cover both of the topics in this post, So the first way is not something that I like and honestly I have never used it as well.

I discovered it while researching deeper for this post and it was not even completely documented, So I will link the orginal documentation and my reference.

So that if you came looking for that resource and I really don’t want to disappoint you with my knowledge, let’s get started then

So, I will give you a summary of what we are trying to do here, we are using package called digiweb and a modified version of pythoninsidehtml.zip that was provided in the documentation.

Here’s the link to the article that I found and let’s start writing some code, the setup and everything is inside the article so, first read this article and open the link for setting up.

Writing python code

If you are going to write a python code then you should use brackets like this. “<% YOUR VALUE %>”

<% import random %> <% myvar = random.randint(0,5) %>

this will only run the code but not display anything in the HTML pages.

Displaying python variables

Let’s continue the previous code and also the display the int with some text of our own and remember the brackets for displaying have to be this way. “<%= YOUR VALUE HERE %>”

<% import random %> <% myvar = random.randint(0,5) %>

Here is my random number that is <%= myvar %>

Now this will display the output in our html page and there we go we did it.

Creating a for loop to our documents

So, for loop it is as same as the below one but the problem is that in python indentation is really must and in HTML indentation is only for the readability.

It is simple and easy but I will try my best to explain you

<% for num in range(10) %>

<%= num %>

<% end %> We start the for loop as normal but to end the for loop we use end keyword here to tell the compiler that till here only you have to indent. So now we will move on to using HTML in python with Jinja and it is my favourite and recommended one and I recommend only what I know is best out there for you mates. ## Python Inside HTML With Jinja (Recommended) So, let’s dive into see what really jinja is and how does it work? for that am gonna show you with one of the web library in python called flask, probably most of you all heard about flask and Django they are the two popular web framework and library. We are using flask in this tutorial for showing how easy it is to use jinja for HTML templating. Let’s get started. I am using a virtualenv and if you want to learn about it I will definently post an article, ├── requirements.txt ├── run.py └── templates └── template.html This is the file structure you should follow and write this command in your shell and command prompt. pip install flask In case if you want to also activate virtual env then type this following commands python -m venv env ## If you're on windows .\env\Scripts\activate ## If you are using mac source env/bin/activate So in our run.py file please use these commands and I will say step by step why we use it and stuff. from flask import Flask, rendertemplate app = Flask(\_name__) @app.route("/") def template_test(): return render_template('template.html', my_name="Sathak Uzham", what_iknow=["Python", "Javascript", "Dart"]) if \_name__ == '__main__': app.run(debug=True) So we are importing flask and a function from it called render_template, then we set a variable called app and this can be anything, for consistency I use app. then we are using a decorator to say the route is at root which means at the “/” home of our localhost. then we start defining a function and you can name your function whatever. inside that we are calling render_template, in which we are passing down two variables called my_name and what_i_know, moreover you can name it whatever until it is valid python variable name. So let’s get started in our HTML file. <!DOCTYPE html> Flask Tutorial

Hello world

Now run the below code in your shell and open http://127.0.0.1:5000/ in your browser. python run.py ### Display Variables In Page Now if you want to print a value that we already sent through our run.py file then we can access it via the variable name and let’s see it in action. Use this {{ VARIABLE NAME OR SOME CODE }}. <!DOCTYPE html> Flask Tutorial

I'm {{ my_name }}

Hello world

Yea!!! We did it and let’s see how to perform small arthmetic operations. It is the same as that use it in between the brackets like this {{ 10 + 7 }} <!DOCTYPE html> Flask Tutorial

I'm {{ my_name }}

My age is {{ 10 + 7 }}

Hello world

Now let’s get into some advanced stuff. ### For loop in Jinja To start looping in jinja we need array or tuples or something that can be iterated. So on our run.py do you remember we added an array called what_i_know. We will loop through it and I will paste the code below and explain then. <!DOCTYPE html> Flask Tutorial

I'm {{ my_name }}

My age is {{ 10 + 7 }}

Hello world

<p>Programming languages I know</p>
<ul>
    {% for lang in what\_i\_know%}
    <li>{{ lang }}</li>
    {% endfor %}
</ul>

{% for lang in what_i_know%}

  • {{ lang }}
  • {% endfor %}

    Here is what we are doing in closer look, We done is that normal for loop stuff but we have added an endfor at the last to indicate the end of the loop.

    Between the loops you can enter anything and it will display for the number of items that are in the list. Here is the result of it.

    Conclusion

    Finally, we have completed learning how to embed HTML into our python file and I really enjoyed writing this article, It’s almost 3 hours now to complete writing about this and I literally loved every section of it.

    If you want I can continue a part 2 with advanced stuff like routing and many more stuffs. Even if only one person wants I will do it for you mate.

    Show your support and what language do you use primarily and do you also use HTML with it?

    More from this blog

    Artistic Programmer

    33 posts

    Hello there everyone, I am uzham and this is my blog and I am currently working on my own startup and since I had this blog on wordpress. I thought of moving to hashnode and I really like hashnode now