I am new to Django and I have a Django application where once you upload few excel files, it does a background calculation and saves the results in an excel file. I need to show those excel files with minimal processing in the Django home page as datatable. I tried django-excel package with Iframe to do so. I am not not able to get the page. Please suggest if the code below needs to be modified. Is there any other ways to solve this problem?
In views.py:
import django_excel as excel
import pyexcel as p
def show_result_files(request,foldername,outputfilenames):
file_path = path + '/Data/' + foldername
if os.path.isfile(file_path + 'Final_Results.xlsx'):
for afile in request.FILES.getlist(outputfilenames):
combined_sheet = p.get_sheet(file_path + '2020MY EWPR Combined Parts List Final.xlsx')
combined_sheet.save_as(afile.sortable.html)
from IPython.display import IFrame
IFrame("afile.sortable.html", width=600, height=500)
In home.html:
<div class="inline col-md-9" id="showFinalResults">
<form method="POST" name="showCombined" action="">{% csrf_token %}
<button name="showCombined" type="submit" class="btn btn-primary">Combined Parts List</button>
</form>
</div>
<div class="inline col-md-9" id="showFinalResults">
<form method="POST" name="showCombined" enctype="multipart/form-data" action=''>{% csrf_token %}
<div class="col">
<div class="row">
<span class="btn btn-primary btn-file">
Combined Parts List <input name="SparrowFiles" id="Sparrow" type="file" multiple="">
</span>
<button name='upload_Sparrow' type="submit" class="btn btn-secondary js-upload-files" data-toggle="modal" data-target="#modal-progress_sp"">Go</button>
</div>
</div>
</form>
</div>
What I want the resulting excel file displayed in the Home page when a "showCombined" button is clicked
There are various options, depending on whether you want the spreadsheet data to be accessiblew to users who cannot use excel, or not.
The most general is for the button to be a hyperlink that invokes a Django view which renders the spreadsheet data as an HTML <table>. There is a completely awesome JavaScript called DataTables that can make such a table almost as functional as Excel insofar as display is concerned. Very easy to use: start simple, then add tweaks until perfected. Absolutely loads of examples available (and much support with it on StackOverflow).
Another is for the computation which saves its results as Excel to also save as pdf alongside, and to serve up the pdf in that hyperlink.
Finally, you can make the target of the link the xlsx file itself, and the user's browser will ask how to open it if Excel is not already set up as default (which it probably will be, on a Windows box. On a Linux box LibreOffice Calc will probably accomplish much the same.).
Related
I have two pages index.html having code as following
<form action="/" method="POST">
<div class="form-group">
<textarea class="form-control" name="text" placeholder="Enter Text" rows="6"></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
<button>
</div>
<div class="col-md-3 col-sm-1"></div>
<input type=button onClick="location.href='upload.html'"
value='click here'>
</div>
<button>
It has js and css in head. As you can see there is a button to go to upload.html page, which contains code to upload file as following
<body>
<h1>File Upload</h1>
<form method="POST" action="" enctype="multipart/form-data">
<p><input type="file" name="file" accept=".pdf"></p>
<p><input type="submit" value="Submit"></p>
</form>
</body>
If I open index.html page in a browser and click on click here button it redirects me to the intended page, But when I run Flask app and try to click on this button it gives 404 error. Right now I haven't created any Flask method for this file. I have tried to add absolute path to upload.html file and ./upload.html and almost every method to create a button mentioned here. I also tried to redirect it to a blank page and index.html itself but every time the error is same. It is only giving me error with local HTML files if I give an address of some website it redirects me to it. Can someone help me with what I need to do to make it work?
I think you need to create a route and point the input element to the new page. Right now that won't work within Flask because the app does not "see" the upload.html page.
so you would first create a route in your routes.py or app.py
#app.route(methods=["GET", "POST"])
def file_upload():
return render_template("upload.html")
and then in your index.html you would point the input to this route:
<input type=button href="{{url_for('file_upload')}}" value='click here'>
just remember, url_for as a function points to the function name, not the html template itself. So here you call url_for which in turn calls file_upload which in turn renders upload.html.
So, last few days I was trying to build a website where you input the post data then hit submit and then boom your post appears on the home page/post page.
So, I have created the form but the problem is that I don't know how to send HTML form data to SQLite database so it can be viewed by multiple users anytime.
<form class="posts" action="." method="post">
<h2>Title</h2>
<input type="text" name="title" placeholder="Title">
<h2>Description</h2>
<textarea input class="des" name="description"type="text" placeholder="Description"></textarea>
<h2>Image(Optional)</h2>
<input type="file" name="inpFile" id="inpFile" class="img-btn">
<div class="img-prev" id="imgPrev">
<img src="" alt="Image Preview" class="img-prev__img">
<span class="img-prev__def-text">Image Preview</span>
</div>
<input type="submit" value="Submit">
</form>
I think that you should take a look at some Python's web-frameworks like Flask or Django first, so that you can understand this subject a little clearer.
I am developing a Python-Django website and want to take input from the user and use it. So i could use this code in my HTML page:
<form method="get">
<fieldset><legend>Form</legend>
<table>
<tr><td>Name </td><td><input type="text" name="name"> </td></tr>
<tr><td></td><td><input type="submit" value="Submit"></td></tr>
</table>
</fieldset>
</form>
Thus, could get a link ending with the input. But how do I use that input on my tag.py or even other HTML pages? Would be glad if someone helps.
You have to create form and then place it in table cell.
Read documentation about Django forms
I need to get the user input in a textbox and process it using my python script when user presses Submit button. Unfortunately I am using an external server that does not support CGI. Is there any other way I can do this? This is my sample HTML.
<body>
<form name="form1" method="post" action="script1.py">
<div id="heading"><big><big><big>My analysis</big></big></big></div>
<textarea cols="50" rows="1" name="Query_text" id="Query_text"></textarea>
<div id="Button"><input name="Submit" value="Submit" type="submit"></div>
</form>
</body>
You can do the form processing with Javascript but hopefully you don't want to save this input beyond the next page the person accesses. Another old school option is emailing yourself the contents of Query_text. But honestly this is a lot more work than you want to do and not worth the effort (for most people). What you need is a new web host.
I'm using a Tornado server running with Python, whose job in convert a .svg file in several font formats. For now I managed to make it generate a fontpack in a .zip file by clicking on a button. My Python code links with html form by using self.request.files['filearg'] where filearg is the name of the file selected in an <input type="file"> box.
But I'd like to make this submit button communicate with a text box in which I'd fill the output format I want my file converted in.
Basically, what I'd want in my html form would look like this:
<form enctype="multipart/form-data" action="/upload" method="post">
File: <input type="file" name="filearg" />
<input type="text" value="" name="format"/>
<input type="submit" value="Generate font(s)" />
</form>
I managed to do it with a Node.js server (with fields.format and so on), but I can't find any way to do this in Python.
I've heard about the CGI forms in HTML, which can permit to get this kind of content. But as if it's not the kind of form I chose, I wonder whether it can be implemented with my actual form.
The Tornado documentation explains how to get the values of form fields, using get_argument:
format = self.get_argument("format")