I am trying to serve multiple HTML Pages to a single page and then serve that final single page as a PDF. I have a total of 95 pages and I have already achieved this using the following stack;
Python/ Flask
WeasyPrint HTML to PDF Creator
Jinja Templating using include
{% include 'page1.html' %}
{% include 'page2.html' %}
...
...
{% include 'page95.html' %}
Heroku deployment
gunicorn and nginx along side Flask in production
My problem is, the final page takes more than 80seconds to display as a PDF(i.e: the final html page containing 95 other html pages). And Heroku can maintain a connection only for 28-30 seconds. Is there any way I can speed up this process of serving the final PDF?
Will multi-threading help this? (I may have to read up on how to do this - not an expert) I already have this in my app
app.run(threaded=True)
Apologies if I am using any unclear terms here.
After trying out a few things, I think the best way to reduce the time is to simply use Reportlab and make PDF out of single pages. Then I will be using
pyPDF2
to merge all those single pages into one single PDF file to download. I will mark this as the answer, if I am able to execute it successfully!
Related
I have a Python web app that uses Flask and Django (among other things). The app currently routes to quite a few HTML pages. A few of those HTML pages are forms that post to PHP files where the data is submitted to a db.
When launching with VSCode, all python and HTML files work great but the PHP file doesn't execute and obviously doesn't submit the data to the db. Conversely, when running the HTML/PHP files directly in Chrome, the data is submitted but the HTML form is no longer dynamic and the templates aren't successfully extended to the HTML file.
Is this goal even achievable? Should the PHP document be translated to a different language?
You've got two questions here- is it doable, and should you do it. The answer to the first is yes, it absolutely is doable- but the answer to the second is that you should not do it as it will be a maintenance nightmare and will introduce complexities into the system that you're probably going to want to avoid.
I've always linked webpages in html but recently saw someone use python to link html pages. Is there an added benefit to linking in python/flask over html ?
If you have template in flask application and the html link is resolved as variable obtained with url_for or some similar way - it's preferred way of doing using links because if flask view route will be modified the link in html will be modified to (automatically), hence it's more stable variant as it reduces possibility for human factor error .
I was going through the library NVD3 and have found this as an example on the website:
d3.json('cumulativeLineData.json', function(data) {
nv.addGraph(function() {
var chart = nv.models.cumulativeLineChart()
.x(function(d) { return d[0] })
.y(function(d) { return d[1]/100 }) //adjusting, 100% is 1.00, not 100 as it is in the data
.color(d3.scale.category10().range())
.useInteractiveGuideline(true)
;
chart.xAxis
.tickValues([1078030800000,1122782400000,1167541200000,1251691200000])
.tickFormat(function(d) {
return d3.time.format('%x')(new Date(d))
});
chart.yAxis
.tickFormat(d3.format(',.1%'));
d3.select('#chart svg')
.datum(data)
.call(chart);
//TODO: Figure out a good way to do this automatically
nv.utils.windowResize(chart.update);
return chart;
});
});
The image is something like this:
Check the live demo here: http://nvd3.org/examples/cumulativeLine.html
The json file is here: Json file for example
Now I was willing to include such charts in the django example. So I went on checking the implementation of Django-NVD3. But I could not find anything related to it and the documentation written by author is not understood by me.
Please let me know how I can include d3 chart in the django frame in real time.
Django largely is backend framework for python. That means it creates responses to html requests. These responses are rendered using templates that contain the html code which is being created on the fly.
nvd3.js or d3.js for that matter live on the frontend, i.e. the user's browser. That means all nvd3.js html and javascript code (like the one you quote) go into the Django template.
To use nvd3.js you will have to load the d3.js and nvd3.js javascript libraries and corresponding style sheets. (On how to do this, see the respective documentations.) These elements need to go to a different place in the template (html where the chart is supposed to go, css into the header and javascript at the end of the body).
django-nvd3 is a django app that is used to simplify the use of nvd3 on the front end. It is one option to work with Django and nvd3.js. It defines template tags that will include the required javascript code and style sheets in your template. The first tag include_chart_jscss will do exactly this and is supposed to be used in the <head> section of the tempalte. The second tag load_chartwill generate the javascript you quoted actually creating the chart. The third tag (include_container) will insert the required html div elements (which you did not quote in the questions). By passing the latter tags the same name the browser knows on which div tag to apply the javascript code. It does not help to distribute the code bits over the template. Also, it does not generate the code. This is left to a different package python-nvd3 which itself relies on a template engine (Jinja2) which likely is different from the one you use with Django. In a nutshell: django-nvd3 solves the problem of generating javascript code for nvd3 charts but not the problem of how to distribute that code in django templates.
I suggest Sekizai to better distribute code bits. It allows to split html, css and js bits of a page in separate blocks. Then you can use simple includes to add a chart to your web page. In the included file you may surround the required css code by {% addtoblock css %} and the required
javascript by {% addtoblock js %}. When using sekizai the base templates need to have these block ("js" and "css") defined, see the Sekizai documentation.
Of course it is a matter of opinion, but I prefer using sekizai for distributing the nvd3.js code bits in the template and making charts available as includable templates to Django (thereby refraining from the costs of a second template engine). My include templates contain the raw nvd3 code taken, e.g. from one of the examples.
I have compiled three gists which assume you have sekizai installed and included in your INSTALLED_APPS settings:
Python code for the view function. Remember to include the view function in urls.py. The code assumes that the file with the json data is in a static folder.
A template called base.html which does include the html code of the page to be rendered. It has a title and then includes the chart.
A template for the chart which is a 1:1 copy of the code in the question except that some parameters are dynamically loaded (source file, ticks).
I´m new to web dev,
and I was wondering if it´s possible to make a website, that just need to present information of a company (HTML), in just one view.
Like rendering the entire bootstrap in one view.
Yes, you can serve your HTML code through a TemplateView.
So if your entire single page application sits in home.html you could definitely do this. But there is no point in using Django for only that purpose. You would rather want to serve your static HTML page from a classic web server like nginx or apache.
I don't know why would you want to do that.
You can use different html files which will be served as your website templates. You can also extend the files using a simple base.html file. This will help you if you want to open other links when people click on different links on the website.
See this example: https://github.com/singh1114/Djangosite/tree/master/duggal/webportal/templates/webportal.
For using this you have to know more about views and urls.
For making scrollable things, you need to know the concept of ids in HTML.
For example
http://yoursite.com/#your_name will try to find id your_name in the HTML. This way you can create scrollable things.
I'm switching to Pyramid from Apache/PHP/Smarty/Dreamweaver scheme.
I mean the situation of having static site in Apache with menu realized via Dreamweaver template or other static tools. And then if I wanted to put some dynamic content in html I could make the following:
Put smarty templates in html.
Create php behind html with same name. Php takes html as template.
Change links from html to php.
And that was all.
This scheme is convenient because the site is viewable in browser and editable
in Dreamweaver.
How can I reproduce this scheme in Pyramid?
There are separate dirs for templates and static content. Plus all this myapp:static modifiers in hrefs. Where to look up?
Thank you for your advices.
There is no smarty port for Python. So you would have to start using another template syntax, such as mako or chameleon
To do this, you would setup your view_config to respond to the url, end tell it to use the corresponding template.
If you want to do this, you would simple change your code. But this is not necessary, pyramid will process your requests, whether the url contains .html, .php, .python, /, or whatever.
You could still edit the templates in Dreamweaver I guess.
Only really static pages would be linked using static_url. If it is html that you mean to make into a template, it might be easiest to just start of with a template right away, without any dynamic content in it.
This is from the URL dispatch tutorial:
# in views.py
#view_config(route_name='view_page', renderer='templates/view.pt')
def view_page(request):
return {}
# in __init__.py
config.add_route('view_page', 'mypage.html')
You can build a small web application which uses traversal to serve html documents from a directory. Here's more explanations about how traversal works.
Then you can programmatically render those documents as Chameleon templates, using PageTemplateFile for example. This would allow you to include, say, common header/footer/navigation into every page.
This would mean that every page in your site will be in fact dynamic, so that would incur a small performance penalty for every page regardless of whether it has dynamic content or not, but you should not be concerned with this unless you're building the next Facebook. :) However, this approach would allow you to have a plain html document corresponding to every page in your website which you'll be able to edit using Dreamweaver or any other editor.
This is somewhat a different answer than ohters but here is a completely different flow.
Write all your pages in html. Everything!!! and then use something like angularjs or knockoutjs to add dynamic content. Pyramid will serve dynamic content requested using ajax.
You can then map everything to you html templates... edit those templates wherever you want since they are simply html files.
The downside is that making it work altogether isn't that simple at first.