How can I run an html page from python using CGI - python

I am on a project where I have connected python and html using CGI. I am now able to receive inputs from html and save it to MySQL database through python. My project is a ticket booking website. I now have to show the user that their tickets have been booked, and also, I will have to show them their fare. Though I can simply type the code in printing statements to print the data in html, it doesn't look good from the user side, as I can't style it. Has anyone got any solutions? Can I directly start any html page like that?
I tried to print the fare, and I was able to do it. But the page looked very boring without any styling. I want to open an html page from my python file (because I can style it)-, but I am not using any web application like Flask or Django. How can I do it?

To run your Python script as a CGI script, you can either:
Name your script with a .cgi file extension (example.cgi).
Configure Apache to recognize and allow the .py file extension as a CGI script.

To run your python script as a CGI script,you can either:
Name your script with a .cgi file extension (example.cgi).
Configure Apache to recognize and allow the .py file extension as a CGI extension

"Run a HTML page" doen't make much sense. Sounds like your Python script is already running under CGI, and it's html output needs to include style information.
Print out the style information as well as the values into your html.
Or print out a link into the html header to include a seperate style sheet.
Or use python to load a html template from a file and make some string substitutions with your data before printing out the edited template.

Related

How to link to python file inside html code

So I have created a game (of sorts) in python that I want to embed into an html webpage so I can add UI features. I have used the tags to do this but I am having issues with importing packages and also it clutters up the code. So is there a way to link to the python file instead, like I would a JS or CSS file?
I apologise in advance for any ambiguity or poor phrasing in my question, I am new to programming and don't really have anyone to turn to when I need help so I have to use SO for even the most minor errors.
If you’re using a <py-script> tag, you can use the src attribute to reference a URL where the relevant python code is located. In this case, any code written within the tag itself (that is, in the HTML page) is ignored. For example:
<py-script src="some/url/with/code.py"></py-script>
Note that the attribute is a URL, not a local file path, so you’ll likely want to use a small server program to make the python file available on the network. Running python -m http.server from the command line will do.

Import python projects to a HTML page

Supose I have a python game and I want to "post" it on a site like Friv that I am making. Is there any way
for me import the "game.py" to the "site.html" and it show when I enter the site? I made a search and found to use django, but I would need to pass all the html code that I already have to other aplication.
The language of browsers is JavaScript.
There is a project called PyJs which translates Python code to JavaScript and is useful in your case that you want to run Python code inside web browsers.
Finally you can use your resulting JavaScript files to fill up your HTML page.
In addition to PyJs, there are numerous other projects that "run Python code in a browser" like Brython. However, any of them have not been standardized and if you want a robust game in your browser, use JavaScript!
There are number of projects that compile python into JavaScript in order to be run on browser.
Here are two links that might help
Web Browser Programming: https://wiki.python.org/moin/WebBrowserProgramming
PyGame Trinket: https://trinket.io/features/pygame
The way I integrate python code in an html is to use templating language like jinja2 but if you want to write full python code in html then use need to use a transpiler like PyJS but since you want to integrate the same code in multiple program, why not use FLASK it is much more easier.
and make an api. Django is an option but it has a steep learning curve. you can make the UI using HTML and get the data from python using API.

How to generate a static .html with python

I'm looking for a python solution to create a static .html that can be sent out via email, either attached or embedded in the email (ignore this latter option if it requires a lot more work). I do not have requirements for what regards the layout of the .html. The focus here is in identifying the less painful solution for to generate an offline .html.
A potential solution could be along the lines of the following pseudo-code.
from some_unknown_pkg import StaticHTML
# Initialise instance
newsletter = StaticHTML()
# Append charts, tables and text to blank newsletter.
newsletter.append(text_here)
newsletter.append(interactive_chart_generated_with_plotly)
newsletter.append(more_text_here)
newsletter.append(a_png_file_loaded_from_local_pc)
# Save newsletter to .html, ready to be sent out.
newsletter.save_to_html('newsletter.html')
Where 'newsletter.html' can be opened in a whatever browser. Just to provide a bit more context, this .html is supposed to be sent out to a few selected people inside my company and contains sensible data. I'm using plotly to generate interactive charts to be inserted in the .html.
Possible solution here
Seems package in that answer is exactly you want. Docs: http://www.yattag.org/
Another pretty nice package here.
Start your python module with by importing sys module and redirect stdout to newsletter.html
import sys
sys.stdout = open('newsletter.html','w')
This will redirect any output generated to the html file. Now, just use the print command in python to transmit html tags to the file. For eg try:
print "<html>"
print "<p> This is my NewsLetter </p>"
print "</html>"`
This code snippet will create a basic HTML file. Now, you can open this file in any browser. For sending email you can use email and smtplib modules of python.
The Dominate package looks like it provides a simple and intuitive way to create HTML pages. https://www.yattag.org/

python/django, firefox: Is there a way that I can use python code to call firefox's functionality

I've been searching for this for some time, but I couldn't seem to find a way to achieve this.
What I want to do is that I need the functionality of web page to pdf conversion from firefox. Right now the web page is generated in my django application and I use an open source software called "pisa"(or "xhtml2pdf") to get pdf report. However, it only supports very limited css styles, and some of the images are not rendering properly. After trying several possibilities, I found that firefox gives exactly what I want though printing web page to pdf file option in the brower gui, so I'm wondering if I could use python or command line to make firefox does the same thing. I would be very appreciated if somebody can pointing me to some resources for firefox commands or python api. Thanks.
To print from the command line with Firefox, you need to install an extension. One such extension is
Command Line Print by torisugari.
This extension allows you to print URLs immediately, without user interaction. This can be useful to convert html pages to PDF for example.
You first have to install the extension from http://torisugari.googlepages.com/commandlineprint2
After you've properly installed the extension, you can start using Firefox as command line printer.
Usage:
$>firefox -print http://www.example.com/index.html
$>firefox -print http://www.example.com/index.html -printmode pdf -printfile foobar.pdf
$>firefox -print http://www.example.com/index.html -printmode PNG
from here Command Line Print - torisugari -> https://sites.google.com/site/torisugari/commandlineprint2
now you must add your page like 127.0.0.1/yourpage with django webserver
so with loop and address you can print all page
Take a look at wkhtmltopdf.
It is a simple command line Utility, using the WebKit rendering engine, which is also used by Google Chrome and Apple Safari.

How do i output a dynamically generated web page to a .html page instead of .py cgi page?

So ive just started learning python on WAMP, ive got the results of a html form using cgi, and successfully performed a database search with mysqldb. I can return the results to a page that ends with .py by using print statements in the python cgi code, but i want to create a webpage that's .html and have that returned to the user, and/or keep them on the same webaddress when the database search results return.
thanks
paul
edit: to clarify on my local machine, i see /localhost/search.html in the address bar i submit the html form, and receive a results page at /localhost/cgi-bin/searchresults.py. i want to see the results on /localhost/results.html or /localhost/search.html. if this was on a public server im ASSUMING it would return .../cgi-bin/searchresults.py, the last time i saw /cgi-bin/ directories was in the 90s in a url. ive glanced at addhandler, as david suggested, im not sure if thats what i want.
edit: thanks all of you for your input, yep without using frameworks, mod_rewrite seems the way to go, but having looked at that, I decided to save myself the trouble and go with django with mod_wsgi, mainly because of the size of its userbase and amount of docs. i might switch to a lighter/more customisable framework, once ive got the basics
First, I'd suggest that you remember that URLs are URLs and that file extensions don't matter, and that you should just leave it.
If that isn't enough, then remember that URLs are URLs and that file extensions don't matter — and configure Apache to use a different rule to determine that is a CGI program rather than a static file to be served up as is. You can use AddHandler to add a handler for files on the hard disk with a .html extension.
Alternatively, you could use mod_rewrite to tell Apache that …/foo.html means …/foo.py
Finally, I'd suggest that if you do muck around with what URLs look like, that you remove any sign of something that looks like a file extension (so that …/foo is requested rather then …/foo.anything).
As for keeping the user on the same address for results as for the request … that is just a matter of having the program output the basic page without results if it doesn't get the query string parameters that indicate a search term had been passed.

Categories

Resources