I needed to view reStructuredText files in JupyterLab, and the best I found so far was #akaihola's answer to a related issue on github.
I added a work around that would allow rendering the file without viewing the source, provided below.
In case anyone else may need it, here's the solution I am working with for now:
import docutils.core
import docutils.writers.html5_polyglot
from IPython.core.magic import register_cell_magic, register_line_magic
from IPython.display import HTML
#register_cell_magic
def rst(line, cell):
"Render ReStructuredText"
writer = docutils.writers.html5_polyglot.Writer()
return HTML(docutils.core.publish_string(cell, writer=writer).decode('UTF-8'))
#register_line_magic
def rstfile(filename):
"Render ReStructuredText"
writer = docutils.writers.html5_polyglot.Writer()
with open(filename, 'r') as file:
cell = file.read()
return HTML(docutils.core.publish_string(cell, writer=writer).decode('UTF-8'))
To view the rst file, without the source:
%rstfile <your-rst-filename>
To use the original solution, as an rst cell, showing both the ReStructuredText source and the rendered output:
%%rst
============
Main title
============
Some **heavy** markup.
Related
I would like to view an image interactively in F# jupyter notebook similarly to how I can do this in python:
from PIL import Image
Image.open("image.png")
and that shows an image.
Is there a straightforward way to do this?
EmguCV, OpenCVSharp and ImageSharp all don't work together with the plotting libraries like Plotly.NET or XPlot to provide this functionality so I can't get something like matplotlib's pyplot.imshow.
In a notebook, you can display an image like this:
#!fsharp
// Load iamge
let data = File.ReadAllBytes("E:\\Downloads\\myImage.png");
// Convert so we can display it as HTML
let b64 = Convert.ToBase64String(data);
HTML($"<img src=\"data:image/png;base64,{b64}\"></img>") // last call without ; gets displayed
// Alt, this one has a semicolon:
// display(HTML($"<img src=\"data:image/png;base64,{b64}\"></img>"));
I'm trying to download all the comics(png) from xkcd.com and here's my code to do the download job:
imageFile=open(os.path.join('XKCD',os.path.basename(comicLink)),'wb')
for chunk in res.iter_content(100000):
imageFile.write(chunk)
imageFile.close()
And the downloadeded file is 6388Bytes and cannot be opened while the real file from link is 27.6Kb.
I've already tested my code line by line in shell, so I'm pretty sure I get the right link and right file.
I just don't understand why the png downloaded by my code is smaller.
Also I tried to search why this is happening but without helpful information.
Thanks.
Okay since you are using requests here is function that will let you download a file given a url
def download_file(url):
local_filename = url.split('/')[-1]
# NOTE the stream=True parameter
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
return local_filename
Link to the documentation -> http://docs.python-requests.org/en/latest/user/advanced/#body-content-workflow
I have searched a multiple articles, but unable to get iPython (Python 2.7) to export data to a CSV, and I do not receive an error message to troubleshoot the specific problem, and when I include "print(new_links)" I obtain the desired output; thus, this issue is printing to the csv.
Any suggestions on next steps are much appreciated !
Thanks!
import csv
import requests
import lxml.html as lh
url = 'http://wwwnc.cdc.gov/travel/destinations/list'
page = requests.get(url)
doc = lh.fromstring(page.content)
new_links = []
for link_node in doc.iterdescendants('a'):
try:
new_links.append(link_node.attrib['href'])
except KeyError:
pass
cdc_part1 = open("cdc_part1.csv", 'wb')
wr = csv.writer(cdc_part1, dialect='excel')
wr.writerow(new_links)
Check to make sure the new_links is a list of lists.
If so and wr.writerow(new_links) is still not working, you can try:
for row in new_links:
wr.writerow(row)
I would also check the open statement's file path and mode. Check if you can get it to work with 'w'.
I am using knitr to generate reports automatically to a mediawiki page. The report output is in HTML via pandoc. But I am having problems uploading the figures to the wiki site. So I figured that I would use the SVG device and include the code in the final document instead of relying on external documents. However I am having trouble doing that with either knitr or pandoc. Does anybody know about a pandoc or a knitr option that creates the SVG embedded instead of linking to the image? Or even a small shell script that replaces <img src="myFigure.svg"> with the contents of myFigure.svg.
I ended up using a simple python script for the job
from sys import argv
import re
import os
def svgreplace(match):
"replace match with the content of a filename match"
filename = match.group(1)
with open(filename) as f:
return f.read()
def svgfy(string):
img = re.compile(r'<img src="([^"]*\.svg)"[^>]*>')
return img.sub(svgreplace, string)
if __name__ == "__main__":
fname = argv[1]
with open(fname) as f:
html = f.read()
out_fname = fname + ".tmp"
out = open(out_fname, 'w')
out.write(svgfy(html))
out.close()
os.rename(out_fname, fname)
I have generated QR code in openerp using python. It's working successfully. But I Want to display name on qrcode Image. I tried to create in many way, but still i can not get solutions
Python:
a="Client Id: "+vals['client_id']+"\n"+"Client Name: "+vals['name']
qr = QRCode(version=20, error_correction=ERROR_CORRECT_L, box_size=2, border=2)
qr.add_data(a)
qr.make()
im = qr.make_image()
im.save("/home/cryosave_qrcodes/"+vals['client_id']+".png")
Generated QRcode this done by me
But I want show name on qrcode. like following image.
Generated
I tried to create in many way, but still i can not get solutions
Please anyone answer me
This is one way:
We can add text on qrcode, after qr generate.
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
img = Image.open('sample.jpg')
#img = Image.new("RGBA",(100,150),(0,0,0))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("/home/john/coda/Coda-Heavy.ttf", 20)
# draw.text((x, y),"Sample Text",(r,g,b))
draw.text((10,0),"Sample Text",(100,100,100), font=font)
img.save('/home/john/test.jpg')