shiny for Python using add_layer for Popus from ipyleaflet - python

The bounty expires in 2 days. Answers to this question are eligible for a +50 reputation bounty.
MYaseen208 wants to draw more attention to this question.
I want to use m.add_layer for Popus from ipyleaflet in shiny for python (as given here). However, it is not working as expected. My minimum working example is given below:
from shiny import App, render, ui
from shinywidgets import output_widget, reactive_read, register_widget
from ipywidgets import HTML
from ipyleaflet import Map, Marker, Popup
app_ui = ui.page_fluid(
output_widget("m")
)
def server(input, output, session):
center = (52.204793, 360.121558)
m = Map(center=center, zoom=9, close_popup_on_click=False)
message1 = HTML()
message1.value = "Try clicking the marker!"
# Popup with a given location on the map:
popup = Popup(
location=center,
child=message1,
close_button=False,
auto_close=False,
close_on_escape_key=False
)
m.add_layer(popup) # This line is not working
register_widget("m", m)
app = App(app_ui, server)
Wondering what basic am I missing here?

Related

Label fails to render using python.net

I'm trying to rebuild a C# .net form I built in visual studio in Python to better suit my needs. After some googling around, I came across the package python.net/pythonnet and it's exactly what I've been looking for. Unfortunately, for some reason, I'm able to get it to generate a window and change the title of the window, but that's about it. I've tried looking at the package's documentation and .net documentation and I'm struggling to try and figure out why.
This is the snippet of the code I'm trying to use:
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import Form, Label
from System.Drawing import Font, Size, Point
window = Form()
window.Text = "Window Title"
window.Title = Label()
window.Title.AutoSize = True
window.Title.Location = Point(8, 9)
window.Title.Name = "Title"
window.Title.Size = Size(780, 65)
window.Title.Font = Font("Serif", 42.0)
window.Title.Text = "Title Test"
window.ShowDialog()
Currently, all this does is open a blank form window that's titled "Window Title".

Is there a way to get the streamlit canvas image data to show up in the terminal

This is my first python project and a piece of it involves electronic signatures. I've been trying for a while now to find a way to save whatever image is drawn as a file. The problem is it doesn't seem like I can get usable information to even start manipulating in Visual Studio Code.
def signaturefunk ():
st.write("Signature")
canvas_result = st_canvas(
fill_color="#eee",
stroke_width=5,
stroke_color="black",
background_color="",
update_streamlit=False,
height=200,
width=700,
drawing_mode="freedraw",
)
im = canvas_result.image_data
print(im)
I always get back None in the terminal no matter what I draw on the canvas. Maybe canvas_result.image_data just doesn't work how I expect it to. However, if that's the case I don't even know where to go from here.
First of all canvas_result.image_data returns the image in the canvas, you can test this with this simple code:
import streamlit as st
from streamlit_drawable_canvas import st_canvas
st.title("Signature")
def signaturefunk():
st.write("Canvas")
canvas_result = st_canvas(
fill_color="#eee",
stroke_width=5,
stroke_color="black",
background_color="white",
update_streamlit=False,
height=200,
width=700,
drawing_mode="freedraw",
)
st.write("Image of the canvs")
if canvas_result.image_data is not None:
st.image(canvas_result.image_data)
signaturefunk()
Now in order to save the image you can use OpenCV, the code:
import streamlit as st
from streamlit_drawable_canvas import st_canvas
import cv2
st.title("Signature")
def signaturefunk():
canvas_result = st_canvas(
fill_color="#eee",
stroke_width=5,
stroke_color="black",
background_color="white",
update_streamlit=False,
height=200,
width=700,
drawing_mode="freedraw",
)
# save image
if canvas_result.image_data is not None:
cv2.imwrite(f"img.jpg", canvas_result.image_data)
else:
st.write("no image to save")
signaturefunk()

Using windows forms in Python how to change starting position of messagebox

I am using a python install that doesn't accept Tkinter or any other gui library but will accept .Net and windows forms. I have a message box that needs to come on screen to give the user a message but it always comes up dead centre of the monitor. As the user needs to interact with the window directly underneath the message box the user needs to grab the message box and drag it to another part of the screen before interacting with the window underneath and then pressing ok on the message box. Is there any other way to set the message box to appear in a corner of the monitor or elsewhere?
EDIT: I have a form now, but still don't know the method to call to position the window elsewhere than centre.
Initial Code
import clr
clr.AddReference("System.Windows.Forms")
import System.Windows.Forms as WF
WF.MessageBox.Show("Message box in centre ")
Newer code - still doesn't get me a window in the corner
import sys
sys.path.append(r'C:\Python24\Lib')
import clr
clr.AddReference("System.Drawing")
clr.AddReference("System.Windows.Forms")
from System.Drawing import Point
from System.Windows.Forms import Application, Button, Form, Label
class SdnMessageForm(Form):
def __init__(self):
self.Text = 'SDN winform'
self.label = Label()
self.label.Text = "Move drops to datum"
self.label.Location = Point(50, 50)
self.label.Height = 30
self.label.Width = 200
self.CenterToScreen()
self.count = 0
button = Button()
button.Text = "OK"
button.Location = Point(50, 100)
button.Click += self.buttonPressed
self.Controls.Add(self.label)
self.Controls.Add(button)
def buttonPressed(self, sender, args):
self.Close()
form = SdnMessageForm()
Application.Run(form)
print(dir(form))
Result
Creates a message box in the centre of the display
Desired result
A message box which is displayed in a corner.
Acceptable but not desired result
Comment out the self.CenterToScreen() line, this seems to open the form towards the left gof the display, its not custom but its better than I had. I would still appreciate a custom solution where I can position the form on the right upper corner.

curdoc().add_root() causes bokeh plot rendering to fail silently

The bokeh application below is intended to generate a random dataset when the button is pushed. I am trying to serve the app using the bokeh.client style, where there is one session that may be shared between simultaneous viewers.
If I include the line: curdoc().add_root(column(p,button)) the plot will not be in the browser. I get a blank page with happy messages in JS console. If I remove it, I get a static plot, with no button. Can anyone explain what's wrong with my approach here?
I should add that the app works in the other server style with multiple distinct sessions. There I call bokeh serve myapp.py and don't make calls to the session object.
import numpy as np
from bokeh.plotting import figure, curdoc
from bokeh.layouts import column
from bokeh.models import Button
from bokeh.client import push_session, pull_session
points = 100*np.random.rand(3,100)
points_x = points[0].tolist()
points_y = points[1].tolist()
p = figure(x_range=(0,100), y_range=(0,100))
circle_p = p.circle(x = points_x,
y = points_y,
size = 20,
color = "navy",
alpha = 0.5)
ds = circle_p.data_source
#callback function to update circles
def button_callback():
new_data = dict()
new_points = 100*np.random.rand(3,100)
new_data['x'] = new_points[0].tolist()
new_data['y'] = new_points[1].tolist()
new_data['z'] = new_points[2].tolist()
ds.data = new_data
#Add the button widget thingie to trigger the update
button = Button(label="Update")
button.on_click(button_callback)
# Put the button and plot in a layout in document
curdoc().add_root(column(p,button))
#create a session
session = push_session(curdoc())
session.show(p)
session.loop_until_closed()
You just want
session.show()
not
session.show(p)
Because you want to show the whole document, not just the plot. The first version works for me with Bokeh 0.12.6 (the latter also kind of works, but the plot is duplicated twice. My guess is you are using an older version that also had some layout bugs)

Website to image

I'm running Python 3.1 and you would call me an advanced novice :)
My question is simple: I'm trying to make a simple program which asks the users for a URL (or multiple URLs) and then goes to the website and takes a screenshot (of the whole page, not just what can be seen in the browser without scrolling all the way down).
It's simpler then it sounds, I want to use an existing platform on the web, similar to this:
import subprocess
MYFILENAME = "google_screen"
MYURL = "www.google.com"
subprocess.Popen(['wget', '-O', MYFILENAME+'.png', 'http://images.websnapr.com/?url='+MYURL+'&size=s&nocache=82']).wait()
Although this website does not work :(, I'm wondering is it possible to do it with this website and if so, how? If it is not possible, are there any alternatives?
There is a package called webkit2png that you can use for this, its located: here
More information on this blog post
Example from blog post(copied to SO for preservation, read the blog post to understand it if you have issues):
#!/usr/bin/env python
import sys
import signal
from PyQt4.QtCore import *
from PyQt 4.QtGui import *
from PyQt4.QtWebKit import QWebPage
def onLoadFinished(result):
if not result:
print "Request failed"
sys.exit(1)
# Set the size of the (virtual) browser window
webpage.setViewportSize(webpage.mainFrame().contentsSize())
# Paint this frame into an image
image = QImage(webpage.viewportSize(), QImage.Format_ARGB32)
painter = QPainter(image)
webpage.mainFrame().render(painter)
painter.end()
image.save("output2.png")
sys.exit(0)
app = QApplication(sys.argv)
signal.signal(signal.SIGINT, signal.SIG_DFL)
webpage = QWebPage()
webpage.connect(webpage, SIGNAL("loadFinished(bool)"), onLoadFinished)
webpage.mainFrame().load(QUrl("http://www.google.com"))
sys.exit(app.exec_())
Edit: Link to the pyqt4 download page
You can use Selenium to get a screenshot, but it'll only be what is viewed by the browser.

Categories

Resources