I'm new to learning Python and I've been trying to implement a text to image converter that runs on a web page.
I have succeeded in making the functional code that converts the text into image in Python, using the PIL module (i.e., user enters input text at run time and that gets converted into an image and is stored in the hard drive).
Now I want this code segment to work on a web page (something similar to feedback or comments form in websites).
a. That asks the user to enter a string in a text field and, on pressing a button, it goes ahead and converts it into an image and reloads the page.
b. once reloaded it displays the string-converted-image, and also provides the text box again for any new user to do the same.
I started with the Google Web Apps framework, and unfortunately I learnt that Google Web Apps can't support PIL module although it provides an image API, it can't support dynamic image generations based on user input.
So, can anyone guide me as to how I can go ahead and integrate a web page and the Python code that I have ready? So that it works?
Please guide me where I need to look and anything you consider is necessary to know in order to proceed
ahead.
Check out webpy, just about the simplest Python web framework. In the web.py module, import your PIL image converter, grab the text from the submitted form (via web.input()), convert the text and render a page with the new image on it. You should be able to get a simple page up in a dozen or so lines.
You can use any existing web scripting language to build the actual web page itself. Python (Django?), PHP, ASP.NET, Perl CGI will all work. Then if you're using a python based web framework simply call your python code directly after including the function in your code and serve the resulting generated image. Otherwise you can use a system/exec/passthru call to your code inside of a python script and send the text as input getting the source of the image as output.
To put this all into a webpage you would need the following:
A webpage with form and text box
A web scripting language page which serves an image after taking text as input through GET or POST. Example: Django Serve Image
Once the user submits a form it should load the results of the form data being passed to the script in either a frame or div below the form. This can be done in many different ways with Javascript or Frames.
Related
I am working on a team project for a capstone course. We have decided to produce a self-hosted calendar app for college students. One team member has created HTML webpages for said self-hosted server. I need to hook button and text box input and output into a python program. We are using Starlette, but I cannot figure out how to get information from text boxes.
To give an example of what I need to do, the user will input information into a selection of text boxes. That info needs to be read and converted into variables, then sent into an SQL statement. I have the SQL done, I just don't know how to read the info from the HTML boxes and detect button input.
Before answering it is necessary to know if you are using a python framework, ideally it would be enough to change the action of the form and connect it with a python function.
Here I leave you a tutorial to clarify a little bit the panorama.
https://www.youtube.com/watch?v=dlhg8HMZTOk
I am trying to transfer an app i build in R Shiny to Python Code.
It's main use is to set the input parameters for a data analysis to be run which should create and output pdf file.
So far I managed to understand that DASH might be the tool to go with. But is there a way to launch a DASH app in an external window (not in a browser window)? Like the following:
After setting the Input values I want to have a button to click "Run" or something similiar to run the underlying code and generate a PDF Report. How would you do that with Python and do you maybe know a good Tutorial how to build a basic app like this?
I'm not exactly sure what you mean in the first part of your question. You may be able to use an IDE with integrated broswer to view your app during developement if you don't want to use a broswer. But the whole point of dash is to be a web based application, so it would be best on a browser.
Alternately, you can save the rendered output from browser to a .html file if you need to share it without having a live server host your application.
For the second part of your question, for a button click to generate a pdf report, refer to the documentation of callback in dash.
The callback function for the Run Analysis button can then be something on the lines of
def generate_report():
#Code to generate required pdf
# Code to download on client
return link_to_pdf
Hope this gives some pointers in the direction.
I'm working on a project that basically requires me to go to a website, pick a search mode (name, year, number, etc), search a name, select amongst the results those with a specific type (filtering in other words), pick the option to save those results as opposed to emailing them, pick a format to save them then download them by clicking the save button.
My question is, is there a way to do those steps using a Python program? I am only aware of extracting data and downloading pages/images, but I was wondering if there was a way to write a script that would manipulate the data, and do what a person would manually do, only for a large number of iterations.
I've thought of looking into the URL structures, and finding a way to generate for each iteration the accurate URL, but even if that works, I'm still stuck because of the "Save" button, as I can't find a link that would automatically download the data that I want, and using a function of the urllib2 library would download the page but not the actual file that I want.
Any idea on how to approach this? Any reference/tutorial would be extremely helpful, thanks!
EDIT: When I inspect the save button here is what I get:
Search Button
This would depend a lot on the website your targeting and how their search is implemented.
For some websites, like Reddit, they have an open API where you can add a .json extension to a URL and get a JSON string response as opposed to pure HTML.
For using a REST API or any JSON response, you can load it as a Python dictionary using the json module like this
import json
json_response = '{"customers":[{"name":"carlos", "age":4}, {"name":"jim", "age":5}]}'
rdict = json.loads(json_response)
def print_names(data):
for entry in data["customers"]:
print(entry["name"])
print_names(rdict)
You should take a look at the Library of Congress docs for developers. If they have an API, you'll be able to learn about how you can do search and filter through their API. This will make everything much easier than manipulating a browser through something like Selenium. If there's an API, then you could easily scale your solution up or down.
If there's no API, then you have
Use Selenium with a browser(I prefer Firefox)
Try to get as much info generated, filtered, etc. without actually having to push any buttons on that page by learning how their search engine works with GET and POST requests. For example, if you're looking for books within a range, then manually conduct this search and look at how the URL changes. If you're lucky, you'll see that your search criteria is in the URL. Using this info you can actually conduct a search by visiting that URL which means your program won't have to fill out a form and push buttons, drop-downs, etc.
If you have to use the browser through Selenium(for example, if you want to save the whole page with html, css, js files then you have to press ctrl+s then click "save" button), then you need to find libraries that allow you to manipulate the keyboard within Python. There are such libraries for Ubuntu. These libraries will allow you to press any keys on the keyboard and even do key combinations.
An example of what's possible:
I wrote a script that logs me in to a website, then navigates me to some page, downloads specific links on that page, visits every link, saves every page, avoids saving duplicate pages, and avoids getting caught(i.e. it doesn't behave like a bot by for example visiting 100 pages per minute).
The whole thing took 3-4 hours to code and it actually worked in a virtual Ubuntu machine I had running on my Mac which means while it was doing all that work I could do use my machine. If you don't use a virtual machine, then you'll either have to leave the script running and not interfere with it or make a much more robust program that IMO is not worth coding since you can just use a virtual machine.
Is there an easy way to use the Chrome as a GUI to allow a user to input data for further processing in Python. What I want to do is :
The user enters data into a table. The table is 5 rows by 4 columns. The user data is then processed in JScript and a weight average of each row is displayed in column 5 of the table. The user can then adjust the input data based on what shows up in column 5 or accept it.
Assuming the user accepts the data, it is then used in a python script (that is already written) for much more detailed calculations.
The python script is pretty detailed and no something that is easily re-written in JScript.I can prompt the user for each data input but I would prefer to have a simple table where all the data can be entered at once
Newbie here that knows HTML, a bit of JScript and a bit of Python and some R. But not ready to take on GUI programming in TK, QT, GTK, Kivy....etc.
What is the easiest way to do this ?
You can use flask for this.
You can make a python server, and process all the inputs in python.
Here is a great tutorial to start flask
Flast tutorial
Pick a web framework (Flask seems like the simplest to recommend for your case) and implement your page as a view (whose GET request implements a view that sends the JS and web form to the browser, and whose POST request implements a view that does the detailed calculations and sends the final response).
I have an HTML webpage. It has a search textbox. I want to allow the user to search within a dataset. The dataset is represented by a bunch of files on my server. I wrote a python script which can make that search.
Unfortunately, I'm not familiar with how can I unite the HTML page and a Python script.
The task is to put a python script into the html file so, that:
Python code will be run on the server side
Python code can somehow take the values from the HTML page as input
Python code can somehow put the search results to the HTML webpage as output
Question 1 : How can I do this?
Question 2 : How the python code should be stored on the website?
Question 3 : How it should take HTML values as input?
Question 4 : How can it output the results to the webpage? Do I need to install/use any additional frameworks?
Thanks!
There are too many things to get wrong if you try to implement that by yourself with only what the standard library provides.
I would recommend using a web framework, like flask or django. I linked to the quickstart sections of the comprehensive documentation of both. Basically, you write code and URL specifications that are mapped to the code, e.g. an HTTP GET on /search is mapped to a method returning the HTML page.
You can then use a form submit button to GET /search?query=<param> with the being the user's input. Based on that input you search the dataset and return a new HTML page with results.
Both frameworks have template languages that help you put the search results into HTML.
For testing purposes, web frameworks usually come with a simple webserver you can use. For production purposes, there are better solutions like uwsgi and gunicorn
Also, you should consider putting the data into a database, parsing files for each query can be quite inefficient.
I'm sure you will have more questions on the way, but that's what stackoverflow is for, and if you can ask more specific questions, it is easier to provide more focused answers.
I would look at the cgi library in python.
You should check out Django, its a very flexible and easy Python web-framework.