How to write python code to generate simple html report for some scenarios like piping some output

Kasinath Reddy
Analytics Vidhya
Published in
3 min readOct 20, 2020

This is a scenario which i faced in my real life work and it was one of the most exciting work also for generating a simple html report i wrote something complex and i was asked to create a function a bit similar to print and to identify if a table is coming pass a dict into the function

  1. As you can see i have to create a function file_appender which takes input data and output_path as arguments i wanted the function to be free and work with different output_paths so i haven’t restricted it to only output_html_path
  2. As we can see mp_print() function to identify if i am getting a table i am going to pass a dictionary with ‘table’ as key also in dictionary there may be other keys if you need them you just need to do a loop for keys and values and add to op_str which is string concatenation

3.As we can see table_html gives us a table in html syntax i am using it in mp_print if there is a dictionary with a key ‘table’.

So how simple the mp_print function is going to be in use?

So let’s see how the output is gonna look :) Also mp_print only writes to a file in html syntax so you can customize it by adding print statement inside mp_print like print(all_args) so that it will show output in your existing ide or console as well as write to a html file which can be rendered

The output looks decent

During this task when i created a complex type of print function my senior asked me that he said function can be complex but he wanted the freedom close to the print function which my first attempt lacked later i had to implement mp_print with only few conditionals

Its all about exploring,trying,running into errors,fixing em and getting a good output

Hope this helps please check below link for the file the code was for simple purpose the more the purpose the more complex code is going to be in most cases

https://drive.google.com/file/d/13iXKh01bXhj1b2mvodB20aRXxayE_tb3/view?usp=sharing

Happy learning !

--

--