Sometimes I need to type some credentials to access a remote machine from my notebook. I could write them in a cell but then I would need to remove them from the notebook to avoid sharing them by negligence. I was using a simple fonction showing a window tkinter, a pop up. But this solution only works if the notebook server is local. When it is remote, the pop up windows appears on the remote machine and I cannot see it.
IPython allows javascript functions to execute some Python instructions which impact the workspace. All I had to do was to print a form in the page with some javascript. The result can be found here : Having a form in a notebook.
Plutôt que de créer des interfaces graphiques, j'utilise de préférence une page HTML avec du javascript que j'exécute avec un navigateur. Ca prend moins de temps et on trouve facilement sur internet une solution à son problème, voire trop de solutions pour extraire les paramètres d'un url en javascript : http://....?par1=value1&par2=value2. Voici un code qui fonctionne :
Quand Google Reader a disparu, je me suis demandé s'il était compliqué d'implémenter rapidement une sorte de page HTML en local depuis laquelle je pourrais consulter les blogs. Même si j'ai finalement implémenté quelque chose d'un peu plus complexe (pymmails), il est resté quelques essais et bouts de codes. Le code suivant permet de diviser la page en deux, d'avoir d'un côté une liste d'urls, de l'autre, une fenêtre dans laquelle s'affiche le site cliqué. Je m'en sers parfois pour faire des listes de raccourcis sur des outils internes plus facile à transférer d'un ordinateur à un autre que les raccourcis d'un navigateur. Un exemple de ce que cela donne est plus bas.
I needed to convert a HTML string into JSON. After looking for results without any success, I thought doing it myself should be faster than searching. As an exemple:
content = '<html><body><div class="an_example"><p>one paragraph</p></div></body></html>' js = HTMLtoJSONParser.to_json(content) print (js)Will produce this:
{'html': {'body': {'div': {'p': {'': 'one paragraph'}, '#class': 'an_example'}}}}
The implementation of HTMLtoJSONParser follows:
Le code suivant convertit un tableau Excel au format HTML puis stocke le résultat dans le presse-papier (il suffit donc de faire CTRC+V pour le coller où on veut). La macro convertit la zone sélectionnée au format HTML. Voici ce qu'elle donne :
objectif | Python | VBA |
test | if / else | if .. Then / else / end if |
boucle | for .. in .. | for .. = .. to .. / next |
fonction | def | fonction / end function |
Le code de la fonction doit être copié/collé dans un module (ALT+F11 pour voir le code VBA associé à la feuille). Je n'ai pas implémenté beaucoup de styles mais la fonction est facilement modifiable.
' code à copier dans la feuille Excel Function ColorToHtml(ByVal value As String) As String Dim scolor As String scolor = Trim(Hex(value)) While Len(scolor) < 6 scolor = "0" + scolor Wend scolor = Mid(scolor, 3, 4) + Mid(scolor, 1, 2) ColorToHtml = "#" + scolor End Function Sub range_html_to_cliboard() Set rge = Selection Dim res, line, ce As String res = "<table>" + Chr(10) For i = 1 To rge.Rows.Count line = "<tr>" For j = 1 To rge.Columns.Count ce = "<td style=""" If rge(i, j).Interior.color Then ce = ce + "background-color:" + ColorToHtml(rge(i, j).Interior.color) + ";" End If If rge(i, j).Font.color Then ce = ce + "color:" + ColorToHtml(rge(i, j).Font.color) + ";" End If If rge(i, j).Font.Bold Then ce = ce + "font-weight:bold;" End If ce = ce + """>" ce = ce + rge(i, j).Text + "</td>" line = line + ce Next line = line + "</tr>" res = res + line + Chr(10) Next res = res + "</table>" + Chr(10) ClipBoard_SetData (res) End Sub
La fonction ne marche pas tel quel sur les tableaux créés avec des styles, ni même avec une liste filtrée. Pour cela, il faut copier la zone désirée puis faire deux collages spéciaux, le formatting et les valeurs, au même endroit. On sélectionne ensuite la zone collée puis on appelle la macro décrite ci-dessus.
This blog shows many Python script in HTML files with colors. I use the following module to do that: py2html. Well, I downloaded a few years ago and it does not seem to be available anymore. I modified to make it work on Python 3. I also created a second file on the top of it to add other stuff (footer, ...). You can find them here:
A couple of days ago, I wrote a blog (Insérer des formules en code Latex dans un blog) on how to insert Latex formulas in a blog post. Unfortunately, this way does not seem to work all the time. The browser will try to convert formulas using another site each somebody tries to read the post. And sometimes, the latex formula shows up instead of the picture. So, I decided to write a Python program to call http://latex.codecogs.com/latexit.js before publishing the blog post. I only push GIF images and let the latex code as comments. You will find this code latex_svg_gif.py. Basically, it looks for latex formulas, extract them, call the site mentioned below, stores the images, put the former latex code in a comments section and adds a link to the created image.
<div lang="latex_help"> N \frac{c^k-1}{c-1} \sim N \frac{\frac{S}{N}-1}{c-1} \sim \frac{S-N}{c-1} </div>Becomes:
<!-- <div lang="latex_help"> N \frac{c^k-1}{c-1} \sim N \frac{\frac{S}{N}-1}{c-1} \sim \frac{S-N}{c-1} </div> --> <p class="latexcenter"> <img src="giflatex/blog_2013_2013-02-07.html__Nfracck1c1simNfracfracSN1c1simfracSNc1.gif" alt=" N \frac{c^k-1}{c-1} \sim N \frac{\frac{S}{N}-1}{c-1} \sim \frac{S-N}{c-1} " /> </p>The picture name is the concatenation of all symbols in [a-zA-Z0-9].
However, recently, I found another framework which seems to work better than this one. I did not try but it is available with wordpress for example: MathJax.