How Do I import a local HTML file to my Swift Playground Live View? - swift3

The following is the code for importing local html file to swift playgrounds. It is not working but I'm getting a blank image in the Live View:
import UIKit
import WebKit
import PlaygroundSupport
let u = URL(string: "Prac_1.html")
let request:URLRequest = URLRequest(url: u!)
let webView=UIWebView(frame:CGRect(x:0,y:0,width:768.0,height:1024.0))
webView.loadRequest(request) //Loading the request
PlaygroundPage.current.liveView = webView

Copy the HTML file to the Playground's "Resources" folder (go to "Navigator" > "Show project navigator" to display the folder).
Then use Bundle.main.url(forResource:) to fetch the file's URL, like this:
let u = Bundle.main.url(forResource: "Prac_1", withExtension: "html")

Related

how to show pdf from server in a Django view?

I am trying to show/read pdfs from server , but getting erros. Below I have attached my view.py . Please help me to solve it
views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import PDF
def pdf_view(request):
a = PDF.objects.get(id=id)
with open('a.pdf', 'rb') as pdf:
response = HttpResponse(pdf.read(), contenttype='application/pdf')
response['Content-Disposition'] = 'filename=a.pdf'
return response
pdf.closed
you can use use Django templates to show/read pdf on sever. create 'templates' folder inside your django project. inside it create a html file which contain link of you pdf.

How to make a pop up generated file download in Django 2?

I was working on a barcode image generate system and make pdf for printing.
Here is my view.py
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch, cm
from reportlab.platypus import Paragraph
canvas.Canvas('assets/pdf_print/'+barCode+'.pdf')
c.drawImage('1.png',0.9*cm,0,3.5*cm,1.8*cm)
c.drawImage('1.png',4.8*cm,0,3.5*cm,1.8*cm)
c.drawImage('1.png',8.9*cm,0,3.5*cm,1.8*cm)
c.drawImage('1.png',12.7*cm,0,3.5*cm,1.8*cm)
c.drawImage('1.png',16.7*cm,0,3.5*cm,1.8*cm)
c.showPage()
c.save()
I save that pdf file in this path successfully using report lab
assets/pdf_print/
After saving that file in that path, I need to generate a popup download for this file.
How could I do that in Django?
just create a href link the pop with link of view which returns PDF file as response
when the user clicks on that link the browser will prompt to save the file since it is a file response
from django.http import FileResponse, Http404
def pdf_view(request):
try:
return FileResponse(open('foobar.pdf', 'rb'), content_type='application/pdf')
except FileNotFoundError:
raise Http404()

How to set image from external server to kv file

My code works perfectly for image saved in same directory.
#!/usr/bin/kivy
import kivy
kivy.require('1.7.2')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
Builder.load_string('''
<MenuScreen>:
GridLayout:
cols: 1
Button:
on_press: root.val1()
Image:
source: "myimage.PNG"
size: self.parent.width, self.parent.height
allow_stretch: True
keep_ratio: False
''')
class MenuScreen(Screen):
def val1(self):
print "i am executed"
sm = ScreenManager()
menu = MenuScreen(name='menu')
sm.add_widget(menu)
class MainApp(App):
def build(self):
return sm
if __name__ == '__main__':
MainApp().run()
What changes should be made in this code if i want to take image from external source ie
Image:
source: "http://example.com/myimage.jpg"
Obviously this does not work. Please help.
Try using AsyncImage instead. From the documentation:
To load an image asynchronously (for example from an external
webserver), use the AsyncImage subclass:
aimg = AsyncImage(source='http://mywebsite.com/logo.png')
This can be useful as it prevents your application from waiting until
the image is loaded. If you want to display large images or retrieve
them from URL’s, using AsyncImage will allow these resources to be
retrieved on a background thread without blocking your application.

Is it possible to call a view from settings?

I am doing an extension for Django and I want to let the user indicate a view in the settings file.
But if the user include a call to a view as a constant in the setting file, a circular dependency is produced because the view uses django.http that uses the SECRET_KEY from settings:
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
Any way to solve this? Thanks.
Define in your settings the path of your view as a string:
MY_VIEW = 'myapp.views.SomeView'
Then import that where you needed it (not in settings):
from django.utils.module_loading import import_by_path
my_view = import_by_path(settings.MY_VIEW)
Then you can use my_view as you would if you were using:
from myapp.views import SomeView
import_by_path is available since 1.6

embedding generated img inside django template

how would I embedded generated image inside django template?
something like
return render_to_response('graph.html', { 'img': get_graph() })
I don't want this - because it just send image
http.HttpResponse(get_graph(), mimetype="image/png")
I wanted to embed a generated matplotlib image in a django page without making two trips to the django server (one to get the template, one to generate the image). I put the following in my template for the image
<img alt="embedded" src="data:image/png;base64,{{inline_png}}"/>
Then in the view method:
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import cStringIO as StringIO
import base64
num_signed_off = random.randint(0, 10)
num_reviewed = random.randint(0, 50)
num_unreviewed = random.randint(0, 50)
fig = Figure()
ax = fig.add_subplot(111, aspect='equal', axis_bgcolor='b')
ax.pie([num_signed_off, num_reviewed, num_unreviewed],
labels=['Signed Off', 'Reviewed', 'Unreviewed'],
colors=['b', 'r', 'g'],
)
ax.set_title('My Overall Stats')
ax.set_axis_bgcolor('r')
canvas=FigureCanvas(fig)
outstr = StringIO.StringIO()
canvas.print_png(outstr)
ret['inline_png'] = base64.b64encode(outstr.getvalue())
outstr.close()
return render(request, "my_view.html", ret)
The only problem with this is that it doesn't work in IE7 or IE8 - it works with IE9 and newer, thought, and of course with all the standards-based web browsers.
You can base64-encode the image data and use a data URI.
You can map a URL to one of your view functions that returns an HttpResponse with image data and use this URL as the src for your <img> element e.g.
urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^image/', 'views.get_image'),
)
views.py
from django.http import HttpResponse
def get_image(request):
image_data = get_graph() # assuming this returns PNG data
return HttpResponse(image_data, mimetype="image/png")
index.html
<img src="image"/>