How can I store the input of the form in a webpage inside a text file using python?`

I am making a web app with Tkinter, python, and flask. I have created a text area for user input and a submit button which would be used for saving the input. My intention is that when the user clicks on the submit button, a Tkinter file dialog window should open. I tried to implement this idea but it’s not working. So how can I work it out?

EDIT
My intention is to save the inside a .txt file.

@jyotishkaaich,

I’ve never used Tkinter, but I’ve worked a lot with Kivy. One way I think you could do this is to share a database between your flask app and your Tkinter app. When the user clicks the submit button, the input gets saved to the database. You could have your Tkinter app checking a few times a second to see whether a new database entry was made, and if it was, then you could show that on the dialog window. Alternatively, you could have some sort of refresh button on Tkinter that would perform the check on the database and retrieve the input.

As far as binding the button press in your flask app to an action in your Tkinter app, I don’t know if that is possible. It might be, but I don’t have any experience trying something like that.

2 Likes

Sorry for not mentioning that before, but I want to save the input inside a .txt file.

Saving inside a text file is easier.

Just set a conditional for the button press, and then offload the information from the text field into your file.

very loosely something like this:

if request.method == 'POST':
    if request.form['submit_button'] == 'submit':
        input_text = request.form['input_text']
        #write the input_text into your file

<button type="submit" id="btn" name="submit_button" value="submit">Submit</button>
<br>
Text to input: 
<input type="text" name="text_input" value="">
1 Like

That’s not a bad idea, but I would like to present a GUI to the user for saving the input.
Also, I want the user to save it a file of their choice

Ah so you’re not doing any webpage interaction at all then? I guess you would transpose this concept for tkinter, I’m not versed in it though. But the idea would look the same?

I think the idea is to have a text input on a web page with a submit button, and then when the submit button is pressed, Tkinter should bring up some sort of prompt for saving the “file”. @jyotishkaaich correct me if I’m wrong.

YES! , that’s what my idea is. Any suggestions on how to work on it.

What is the point of Tkinter here then? I’m not sure you need Tkinter to save the file. You should be able to pull up the OS’s own file explorer or something. Unless you want to copy the input into Tkinter first so the user can see it.

1 Like

Yes, I’m also a little curious as to the context since it seems involved.

1 Like

Tkinter has a module known as filedialog which provides us various functions for file manipulation. One of the functions which I wanted to use is asksaveasfile(). It opens a window through which the user could save the input in a text file. It actually makes the process of storing the file easier. Due to certain constraints, I don’t want to store it inside a database.

Is the user ever on Tkinter prior to this? From your description, it sounds like the user was on the web, meaning that a Tkinter program wasn’t running at all.

Does that even matter whether the user is ever on Tkinter or not?

Well, if your Tkinter program isn’t running, and you want that program to handle the saving of a file, you’ll have to figure out a way to launch that program on a specific computer from the web when this submit button is pushed in the browser. I imagine this will be rather hard to do, particularly since you’re not downloading a specific file type, but rather just a string of text.

This is saving the data to the website user’s own computer, correct? Because if it’s saving their info into a txt document on your computer, that may be an ethical or even legal violation of some sort.

1 Like

I see. So, how can I implement my idea without using Tkinter. By the way, I was planning to use it in my school project and do not intend to implement it in a server.

You might want to use some javascript for that… Some ideas here: https://stackoverflow.com/questions/13405129/javascript-create-and-save-file

1 Like

Can’t we use python instead of that?

Python is going to deal with the back-end. If you want to store something to a database, it’ll work great. If a user is interacting with a browser interface, I don’t believe python is the most agile in that scenario.

I guess you could emulate python on a browser but that would kind of defeat the purpose of using flask. And there could be some other method I’m not thinking of, but the js answer just jumps right out as the simplest thing to do.

Luckily the javascript isn’t too tricky and it’s well documented. If you have questions with that you can ask here again.

1 Like

To see this in action, check out this example I found online:

https://jsfiddle.net/Kg7eA/

If you look at the JavaScript, you’ll want to change this part to represent the user’s input and the file name you want to give it:

$(function () {
	var str = "hi,dinesh";
	createDownloadLink("#export",str,"dinesh.txt");
});

What if I want to manipulate the input and then store it in the file like encrypting it.