• Stars
    star
    107
  • Rank 313,356 (Top 7 %)
  • Language
    Python
  • License
    GNU Lesser Genera...
  • Created about 12 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Web pages capture using Django & CasperJS

django-screamshot

Build Status Latest PyPI version Number of PyPI downloads Number of Git Hub downloads Format License

django-screamshot is a very naive implementation of Web pages capture with CasperJS (aaAAaah!, phantomjs:))

(See the issues pages for more details about what remains to be done.)

Checkout screamshotter, the simplest Django project powered by django-screamshot.

INSTALL

First make sure you have either the casperjs or phantomjs command in your PATH, using related installation instructions:

Then install the egg :

pip install django-screamshot

USAGE

  • Add screamshot to your INSTALLED_APPS

As a screenshot Web API

Add it to your project URLs :

urlpatterns = patterns('',
    ...
    url(r'^capture/$',  include('screamshot.urls', namespace='screamshot', app_name='screamshot')),
)

You can then obtain a screenshot using the following GET parameters :

url
The website URL to capture. This can be a fully qualified URL, or the name of a URL to be reversed in your Django project. Note: do not forget to encode the url.
selector
CSS3 selector. It will restrict the screenshot to the selected element.
method
HTTP method to be used (default: GET)
width
Viewport width (default: 1400)
height
Viewport height (default: 900)
data
HTTP data to be posted (default: {})
waitfor
CSS3 selector. The screenshot will be performed only once this selector is satisfied. Typical usage: if your page contains a heavy javascript processing, you can add a CSS class on an element when the processing is finished to make sure the screenshot will get the page properly rendered.
render
If render=html, it will return an HTML page containing the image and where the print diaplo box will be automatically opened.
size
Resize image (width x height, e.g: 500x500), need install PIL or Pillow.
crop
If true, then resulting image is cropped to match specified size.

For example : http://server/capture/?url=http://django-fr.org&selector=body&width=1024&height=768&size=500x500

As a template tag

You can include screenshots in your pages using a template tag. It will perform the capture and return the base64 version of the resulting image.

This is very useful if you don't want to expose the capture API publicly.

{% base64capture URL SELECTOR %}

For example, in a SVG template :

{% load screamshot %}
...

<image
   y="200"
   x="300"
   id="imagemap"
   xlink:href="data:{% base64capture "company:map" "#map" %}"
   width="640" />

If you run the capture server on a different instance, you can specify the root url for reversing (default is local) :

SCREAMSHOT_CONFIG = {
    'CAPTURE_ROOT_URL': 'http://127.0.0.1:8001',
}

As a library - render local Django template

Sometimes, you don't have access to the request object. A typical example would be creating a PDF receipt for a customer and emailing it. Both of these tasks can take a while, so it is natural to push them into some queue (like RabbitMQ). But if your pdf-rendering task get's executed, you don't have access to the request object. Don't worry - you can still use screamshot as a library. Here's how.

from screamshot.utils import render_template

# you can either render the template to a TemporaryFile:

with render_template('my-template.html', {'context': 'variables'}) as output:
    # do anything you want with the output
    # like attach it to email message, etc.
    print(output.name)

# or you can specify a path instead:
render_template('my-template.html',
    {'context': 'variables'},
    output='/home/you/rendering.png',
    format='png')

Please note, that in order to load your static files, screamshot will try to replace all STATIC_URL occurence with a local path to your static files (only if they are not hosted via https of course)

Customizing the page rendering

The CasperJS script appends the screamshot CSS class on the body element. You can easily customize the rendering for printing using this CSS marker in your CSS stylesheet:

.screamshot #navigation {
  display: none;
}
.screamshot #main {
  margin: 2em;
}

Capture views with authentication

You can use Basic HTTP authentication in your Django project, create a dedicated user for screenshots and capture the full URL with credentials (http://user:password@host/page/).

Alternatively, you can use a specific view decorator.

Define the authorized IP to capture your pages in your settings :

SCREAMSHOT_CONFIG = {
    'CAPTURE_ALLOWED_IPS': ('127.0.0.1',),
}

And use the provided decorator :

from screamshot.decorators import login_required_capturable


@login_required_capturable
def your_view(request):
    ...

Renderer command and CLI arguments

You can specify which renderer you would like to use, by setting the CAPTURE_METHOD setting. The default value is 'casperjs'. Possible values are 'casperjs' and 'phantomjs'.

SCREAMSHOT_CONFIG = {
    'CAPTURE_METHOD': 'phantomjs',
}

By default, we look for thr CasperJS/PhantomJS binary in the PATH environment variable (like which), but you can bypass this:

SCREAMSHOT_CONFIG = {
    'CASPERJS_CMD': '/home/you/Downloads/apps/casperjs',
    'PHANTOMJS_CMD': '/home/you/Downloads/apps/phantomjs'
}

Please note, that the CAPTURE_METHOD setting specifies which location would be evaluated, i.e. if you set CAPTURE_METHOD to 'phantomjs', PHANTOMJS_CMD would be evaluated.

You can also specify PhantomJS/CasperJS extra-args, such as
--disk-cache=true with the CLI_ARGS setting :
SCREAMSHOT_CONFIG = {
    'CLI_ARGS': ['--disk-cache=true', '--max-disk-cache-size=30000']
}

See related documentation on PhantomJS and CasperJS homepages.

You can also override the capture script. A default implementation uses capture script written for CasperJS. A default capture script for PhantomJS is also provided.

If you have your own script which you would like to use, specify it in CAPTURE_SCRIPT option.

SCREAMSHOT_CONFIG = {
    'CAPTURE_SCRIPT': '/home/you/scripts/capture.js',
}

You can add timeout corresponding to maximum time to wait for CSS3 selector (see waitfor option)

SCREAMSHOT_CONFIG = {
    'TIMEOUT': 7000 #ms 5000 by default,
}

Notes about runserver

If you want to test it using manage.py runserver, you won't be able to capture pages coming from the same instance.

Run it twice (on two ports) and configure CAPTURE_ROOT_URL.

AUTHORS

makinacom

LICENSE

  • Lesser GNU Public License

More Repositories

1

django-leaflet

Use Leaflet in your Django projects
JavaScript
699
star
2

django-safedelete

Mask your objects instead of deleting them from your database.
Python
667
star
3

Leaflet.TextPath

Show text along Polyline with Leaflet
JavaScript
267
star
4

Leaflet.FileLayer

Loads files locally (GeoJSON, KML, GPX) as layers using HTML5 File API
JavaScript
261
star
5

easydict

Access dict values as attributes (works recursively)
Python
260
star
6

django-geojson

django-geojson is a collection of helpers to (de)serialize (Geo)Django objects into GeoJSON.
Python
254
star
7

Leaflet.GeometryUtil

Leaflet utility function on geometries
JavaScript
246
star
8

Leaflet.Snap

Enables snapping of draggable markers to polylines and other layers
JavaScript
195
star
9

landez

Landez operates with tiles, arranges them together into images or builds MBTiles files
Python
135
star
10

Leaflet.Spin

Show a spinner on the map using Spin.js
HTML
133
star
11

DbToolsBundle

A Symfony bundle to backup, restore and anonymize your data
PHP
105
star
12

Leaflet.MeasureControl

Leaflet control to mesure distances on the map
JavaScript
82
star
13

Leaflet.AlmostOver

Trigger mouse events when cursor is "almost" over a layer
JavaScript
72
star
14

Leaflet.LayerIndex

Spatial index of layer objects using RTree.js
JavaScript
64
star
15

Inkscape-Wireframe-Android

SVG UI elements for Android wireframe in Inkscape
58
star
16

Leaflet.RestoreView

Save and restore view from localStorage
HTML
50
star
17

django-templatetag-handlebars

Easily embed Handlebars.js templates in your django templates
JavaScript
48
star
18

django-mbtiles

Serve maps from MBTiles files
JavaScript
45
star
19

drupal_audit

Drupal Audit
Shell
43
star
20

django-mapentity

Generic CRUD for geographic data
JavaScript
40
star
21

php-twig-converter

A simple PHP script trying to convert PHPTemplate files to Twig
PHP
40
star
22

Leaflet.LineExtremities

Displays the extremities of a Polyline
JavaScript
33
star
23

android-archetypes

Maven Archetypes for Android
Java
30
star
24

django-appypod

Render OpenDocument files from templates, using Appy POD
Python
29
star
25

vms

Makina-States based Vagrant boxes
Shell
27
star
26

convertit

A generic format conversion Web API in Pyramid
Python
25
star
27

django-tracking-fields

A Django app allowing the tracking of objects field in the admin site.
Python
23
star
28

tutorials

Collection of tutorials grouped by theme
Jupyter Notebook
21
star
29

geolabel-maker

Data preparation for geospatial artificial intelligence
Jupyter Notebook
21
star
30

django-paperclip

Attach files to Django models
Python
19
star
31

Leaflet.OverIntent

Adds a mouseintent event that guess user intentions to hover layers!
HTML
19
star
32

php-bloom

Simple PHP Bloom filter
PHP
17
star
33

osm-mirror

OSM mirror - full stack
Shell
17
star
34

cassini-gl-style

A Mapbox GL style using the vector tile schema of OpenMapTiles, based on historic Cassini map style.
15
star
35

bureaux-de-vote-reconstruction

Une approche de la reconstruction automatique de la géométrie des bureaux de vote
PLpgSQL
14
star
36

osm-topo

Topographic OSM map style
Python
14
star
37

django-admin-watchdog

A simple Django app to register logs in admin backoffice.
Python
13
star
38

pwa-training

TP : construire une PWA pas à pas
CSS
12
star
39

docker-geodjango

Ubuntu based image with geodjango binary dependencies
Dockerfile
11
star
40

Leaflet.Social

Very simple yet effective sharing buttons Leaflet control
HTML
11
star
41

ODE

A web API and aggregator for human events
Python
11
star
42

drupal-gulpifier

Drupal module to ease Gulp frontend development workflow
PHP
11
star
43

polymaps-extensions

A set of extensions for polymaps
JavaScript
11
star
44

makina-states

Python
9
star
45

makina-slides

Slides Makina Corpus
CSS
9
star
46

php-lucene-query

Minimalistic, feature-rich, PHP Lucene syntax query builder
PHP
9
star
47

ImpOsm2pgRouting

Import OpenStreetMap network and make it available to pgRouting. Take care of updates.
PLpgSQL
8
star
48

drupal-ucms

Multi-site Drupal using the same database, with advanced layout management and content sharing.
PHP
8
star
49

drupal-sf-dic

Brings the Symfony 3 dependency injection container to Drupal 7 along with a limited Drupal 8 API compatibility layer.
PHP
7
star
50

libecw

C
7
star
51

react-pwa

JavaScript
7
star
52

livetitude

Free live map markers sharing !
JavaScript
7
star
53

elections

Cartes électorales, données électorales, découpage des bureaux de vote...
JavaScript
5
star
54

drupal-netsmtp

Drupal SMTP module based upon PEAR::Net_SMTP
PHP
5
star
55

tif2geojson

TourInFrance to GeoJSON
Python
5
star
56

docker-fortivpn

Shell
5
star
57

apubsub

Async PubSub-like PHP API
PHP
5
star
58

Leaflet.Compass

Compass Plugin for leaflet and PhoneGap application
JavaScript
5
star
59

screamshotter

Microservice to capture screenshots of web pages
Python
5
star
60

rdiff-backup

clone of svn repo + debian patches
Python
4
star
61

accessimap-editeur-der

Editeur de documents en relief interactif (DERi)
JavaScript
4
star
62

iban-bundle

International Bank Account Number form type for Symfony
PHP
4
star
63

osm-poi

Extract POIs from OpenStreetMap
Shell
4
star
64

gatsby-plugin-uninline-styles

Un-inline CSS from GatsbyJS generated HTML files
JavaScript
4
star
65

dockerfiles

Shell
4
star
66

drush-progressbar

Drush progress bar
PHP
4
star
67

Leaflet.ForbiddenArea

Prohibit the placement of markers if they are too close to another layer.
JavaScript
4
star
68

moodwalkr

Pedestrian routing application using OpenStreetMap data
Python
4
star
69

Leaflet.PolylineHandles

Allows to grab intermediary points on a polyline
JavaScript
4
star
70

toulouse1830

Visualizing Toulouse in 1830
3
star
71

drupal-quotedown

Drupal QuoteDown module - Support for PageDown editor and Markdown filter
JavaScript
3
star
72

php-layout

Object representation of a display layout, based upon CSS basic functionnality
PHP
3
star
73

hugo-mc-docs

Hugo makinacorpus branded theme for rendering documentation
JavaScript
3
star
74

django-ode

Django ODE
CSS
3
star
75

osmfusion

optimized user interface to import data into OpenStreetMap
JavaScript
3
star
76

exemple-symfony-vuejs

Projet d'exemple pour débuter une application Symfony/Vue.js
PHP
3
star
77

vuejs-d3-skeleton

A skeleton for a D3.js + Vue.js component
Vue
3
star
78

mapbox-gl-path

Create path with or without help of various directions API
TypeScript
3
star
79

ortho44-2018

JavaScript
3
star
80

docker-openconnect

Docker image for openconnect
Shell
3
star
81

drf-examples

Examples for Django Rest Framework tutorials
Python
3
star
82

accessimap-lecteur-der

Lecteur de documents en relief interactif (DERi)
JavaScript
3
star
83

osm-transport-editor

Edit transport relations in OSM
JavaScript
3
star
84

drupal-predictable_tests

WARNING, this project may be moved to drupal.org as soon as "full project access" is granted
PHP
2
star
85

screamshot

Python lib to capture screenshots of web applications
Python
2
star
86

drupal-static-passthrough

Drupal 8 Module - Passthrough static files to Drupal 8
PHP
2
star
87

monitoring-bundle

Simple monitoring tooling and Symfony bundle
PHP
2
star
88

django-activated-mixin

A Django model mixin allowing related objects to be deactivated instead of deleted when they are protected.
Python
2
star
89

corpus-gitlabrunner

Shell
2
star
90

autocomplete-bundle

Autocomplete widget and form type for Symfony
PHP
2
star
91

redis-bundle

Redis API standalone client manager or optional Symfony bundle configurator
PHP
2
star
92

vt-elevation

Client-side elevation profile with vector tiles
JavaScript
2
star
93

ODK_collect

Mobile application for ODK servers
Java
2
star
94

corpus-odoo

Python
2
star
95

php-access-control

Attribute-based access-control micro-framework
PHP
2
star
96

osm-light

CartoCSS
2
star
97

terra-draw-map

JavaScript
2
star
98

jQuery-Mobile-Font-Awesome

Font Awesome integration with jQuery Mobile
CSS
2
star
99

php-goat-domain

Legacy code for implementing entity repositories, alive only for legacy projects maintainance purpose.
PHP
2
star
100

drupalindustry.recipe.drush

Helpers to install drush and generate a "project-specific" drush script
Python
2
star